Python Forum

Full Version: Please help I'm a beginner
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Sorry for possible language mistakes, I'm french
I started programmation for a year but i don't remember anything about it, i'm a total beginner an i want to learn but i don't understand what's wrong with my program, can you help me please.
(I tried to share my project but i'm not sure it works)
Learn classes now. Start with them as simple containers. They will make your life so much easier. Classes are reference. So you never need to use global.
Examples
class Character:
    def __init__(self, max_life, attack, defense, shield):
        self.max_life = max_life
        self.life = max_life
        self.attack = attack
        self.defense = defense
        self.shield = shield
        self.alive = True

player = Character(50, 3, 3, 3)
for k, v in vars(player).items():
    print(k, v)
class Weapon:
    def __init__(self, name, attack):
        self.name = name
        self.attack = attack

    # Represent class as string instead object memory.
    def __repr__(self):
        return str(vars(self)) + '\n'

class Weapons:
    def __init__(self):
        self.dagger = Weapon('Daque', 3)
        self.sword = Weapon('Epee', 6)
        self.bow = Weapon('Arc', 5)

weapons = Weapons()

def main():
    print(vars(weapons))
    print(weapons.sword)
    weapons.sword.attack += 1
    print(weapons.sword)

main()