Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Modifying Classes
#4
Explain classes on my site.

Quick class run down.
self refer to class object. Class object must be created.
player = Player(...) # create a new player object
player2 = Player(...) # create another new player object
player and player2 are to different objects.

If your function effects class only. Then make it part of the class.
Example
#Class
class Player():
    def __init__(self, name="",
                strength=0,
                constitution=0,
                intelligence=0,
                health=0,
                mana=0,
                attack=0):

        self.name = name
        self.str = strength
        self.con = constitution
        self.int = intelligence
        self.hp = health
        self.mp = mana
        self.attack = attack

    # Allows to print and represent it self in other areas.
    def __repr__(self):
        return str(vars(self))

    # Method
    def creation(self):
        name = input('What do they call you?: ')
        self.name = name
        print('Welcome {}, Make your Character!'.format(self.name))

        points = 10
        for points_left in range(points):
            print('1: Add point in Strength')
            print('2: Add point in Constitution')
            print('3: Add point in Intelligence')

            retries = 0
            while True:
                choice = input('Add 1 to a skill: ')
                choice = choice.strip()
                if choice in ['1', '2', '3']:
                    break
                print('Try Again !')
                retries += 1
                if retries > 3:
                    print('You having to much trouble. Seek assistent.\nGood Bye')
                    exit()

            print('You have {} points left!'.format(points - points_left - 1))

            if choice == '1':
                self.str += 1
                print("strength:", self.str)
            elif choice == '2':
                self.con += 1
                print("constitution:", self.con)
            elif choice == '3':
                self.int += 1
                print("intelligence:", self.int)

def main():
    player = Player()
    player.creation()
    print(player)

main()

Couple of other tips.
Try avoid keyword names.
Try to follow style guide.
99 percent of computer problems exists between chair and keyboard.
Reply


Messages In This Thread
Modifying Classes - by Tridium - Aug-02-2019, 11:44 PM
RE: Modifying Classes - by SheeppOSU - Aug-02-2019, 11:59 PM
RE: Modifying Classes - by ndc85430 - Aug-03-2019, 06:06 AM
RE: Modifying Classes - by Windspar - Aug-03-2019, 03:42 PM
RE: Modifying Classes - by Tridium - Aug-03-2019, 05:41 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020