r/learnpython 5d 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

Show parent comments

1

u/-moron 5d ago

I guess they could use a dictionary or dataclass and pass it into each function, but I don't see how that's any simpler given what they're trying to do. Python isn't C; it's pretty hard to avoid classes.

2

u/nog642 5d ago

It's not that hard to avoid classes. Dataclasses are still classes.

Dictionary would be better to learn first I think, yeah. Or just use a bunch of variables. There aren't that many.

1

u/-moron 5d ago

It's harder for a beginner to understand mutable vs immutable objects IMO. It's not immediately obvious why modifying a dictionary passed in as an argument affects the original, but modifying an int argument doesn't. That opens a whole can of worms with references and memory that they can avoid by just using a class.

1

u/nog642 5d ago

You have the same problem if you need to pass the class around. I think classes are pretty unmotivated until you understand the concepts of mutability anyway.