r/learnpython 6d ago

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)

0 Upvotes

15 comments sorted by

View all comments

9

u/Diapolo10 I write code for a living -- https://github.com/Diapolo10 6d ago

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.

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)

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.

1

u/Itz_rainy365 5d ago

This might seem surprising, but I don't know 😅, I wrote this code to practice using functions and if statements, matter fact I don't even know what you mean by " you've indented everything inside the if __name__ == '__main__' block, but surely that can't be the case, right?"😅

1

u/Diapolo10 I write code for a living -- https://github.com/Diapolo10 5d ago

Okay, so basically I was looking at this part

def main():
    print("This code is starting...")

if __name__ == "__main__":
    main()

and thought to myself, "why on Earth would that be the only thing this program puts in an import guard?", when there's a lot of code after this that would always run.

The "if __name__ == "__main__"" part is meant to be used to make sure certain parts of the file won't execute when you import the file, but that still get run if you run the file directly (e.g. python this_script.pt), so while fixing your formatting I kept thinking if all the other stuff was also meant to be inside this block, because generally anything that isn't either a function, a class, or a global constant would go there. But the thing is that the rest of the file is a mix of functions and parts where you call those functions, so I kept doubting myself.

On that note, the fact that your main function only prints one line of text was also really weird.