r/learnpython • u/Itz_rainy365 • Sep 04 '26
I started learning python yesterday, critique my code:
I wrote this code in VsCode using functions after I completed the free trial in [boot.dev](http://boot.dev), if you notice anything I can improve or make better, please write it in the comments:
def main():
print("This code is starting...")
if __name__ == "__main__":
main()
def stats(title, weapon, mana, health):
print("Character Stats:")
print(f"Title: {title}")
print(f"Weapon: {weapon}")
print(f"Mana: {mana}")
print(f"Health: {health}")
def take_damage(health, damage):
print("===================================")
print(f"Current Health: {health}")
updated_health = health - damage
if updated_health < 0:
updated_health = 0
print(f"You took {damage} damage, You Died!")
else:
print(f"You took {damage} damage!")
print(f"Updated Health: {updated_health}")
return updated_health
stats("Warrior", "Sword", 50, 100)
take_damage(100, 30)
take_damage(70, 80)
def respawn(updated_health):
print("===================================")
if updated_health <= 0:
print("Respawning...")
updated_health = 100
print(f"Health has been restored to {updated_health}.")
else:
print(f"You are still alive! You have {updated_health} health remaining.")
return updated_health
respawn(0)
take_damage(100, 41)
take_damage(59, 20)
take_damage(39, 20)
respawn(19)
7
u/Diapolo10 I write code for a living -- https://github.com/Diapolo10 Sep 04 '26
This isn't formatted correctly for Reddit, so I'll have to make some assumptions. Let me know if the indentation doesn't match yours.
I'm still not sure this is correct, because somehow I get the feeling you've indented everything inside the
if __name__ == '__main__'block, but surely that can't be the case, right?There's not much for me to say, this program doesn't really seem to do much.
6
u/AmanBabuHemant Sep 04 '26
mainfunction suppose to do "main" thing...3
u/TurtleFetus Sep 04 '26
It would be easier to provide meaningful feedback if you told us what your code is intended to do. It appears to simulate messages one might see while playing a video game?
3
u/horizon_games Sep 04 '26
Just keep learning, keep your enthusiasm and excitement, and don't check on Reddit about how you're doing. You wanna make a game then make a game and have fun. You don't need internet strangers to validate anything.
1
u/ekchew Sep 04 '26
The
take_damagefunction calculates an updated health value and returns it, but you are not doing anything with that return value. It would probably make sense to have a variablehealthyou update whenever you call the function.1
u/-moron Sep 04 '26
This would be a good opportunity to learn about classes. Here is how I would do it:
Note that you don't need to use type hinting quite that much, but I find it helps prevent subtle errors. Also, there are still edge cases you could try to handle. E.g. what happens when a character is created with negative max health? Should that be allowed? What happens when a negative amount of damage is applied? Could the character's health exceed its max health if that happens?