samedi 11 juin 2016

How Do I Fix My Code So It Can print() Out The Results I Want?


Note: I'm not looking for you to remake my code, I'm looking for tips and links to sites that will help me improve this code. If you want to fix the code I will also accept your answer.

I'm trying to understand how class works but, I'm having a really hard time doing so. I made a class called Stat which contains all of the stats for a given user. All of the stats for the user are, name, level, totexp and role. each role has 3 stats (it should have three but, my code is messed up), health, attack, and defense. Here is the main code (not working):

class Stat():
    def __init__(self, name, level, totexp, role, health, attack, defense):
        self.name = Name
        self.level = 1
        self.totexp = 0
        self.role = Role
        self.health = health
        self.attack = attack
        self.defense = defense
        self.exp = 0
def choose_name():
    while(True):
        Name = input("What is your Dicer's name?:n").title()
        if(any(Bad_Words in Name.lower() for Bad_Words in [])):
            print("That's an inappropriate name! Try again")
        elif(len(Name) < 3):
            print("That name is too short! Try again.")
        elif(len(Name) > 16):
            print("That name is too long! Try again.")
        else:
            break
def choose_role():
    Roles = {
   'mage':lambda:Role(100, 40, 10), #(Health, Attack, Defense)
   'warrior':lambda:Role(100, 30, 20),
   'gunner':lambda:Role(100, 20, 30),
   'archer':lambda:Role(100, 10, 40)
   }
    while(True):
        Role = input("What role do you want to be?nChoices:n1. Magen"
                     "2. Warriorn3. Gunnern4. Archern").title()
        if(Role.lower() in (Roles)):
            return
        else:
            print("That's not a role! Try again.")
self = choose_name()
self = choose_role()

After the main code the Results (Like in the title) or the result code should print out the Stats accordingly to the order of the instances. Here is the result code:

print("Here is your Dicer's information:nDicer's name: {0}n"
      "Level: {1}nExperience: {2}nRole: {3}nRole Stats:n"
      "Health: {4}nAttack: {5}nDefense: {6}".format(self.name, self.level,
                                                      self.totexp, self.role,
                                                      self.health, self.attack,
                                                      self.defense))

When I run the code I get an error:

Traceback (most recent call last):
  File "python", line 37, in <module>
AttributeError: 'NoneType' object has no attribute 'name'

I think I have ideas to why this is happening but I don't know how to fix it. Can you help me with where I need to start on with my code and ways to fix it. Here is the results I wanted (For this example the Name = Bob and Role = Gunner):

Dicer's name: Bob
Level: 1
Experience: 0
Role: Gunner
Role Stats:
Health: 100
Attack: 20
Defense: 30

Aucun commentaire:

Enregistrer un commentaire