2016-06-10 7 views
-1

Hinweis: Ich bin nicht auf der Suche nach Ihnen, meinen Code neu zu erstellen, ich suche nach Tipps und Links zu Websites, die mir helfen, diesen Code zu verbessern. Wenn Sie den Code reparieren möchten, werde ich auch Ihre Antwort akzeptieren.Wie behebe ich meinen Code, damit er die Ergebnisse drucken kann, die ich will?

Ich versuche zu verstehen, wie class funktioniert, aber ich habe eine wirklich harte Zeit dabei. Ich machte eine class namens Stat, die alle Statistiken für einen bestimmten Benutzer enthält. Alle Statistiken für den Benutzer sind name, level, totexp and role. Jeder role hat 3 stats (es sollte drei aber, mein Code ist durcheinander), health, attack, and defense. Hier ist der Hauptcode (nicht funktioniert):

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. Mage\n" 
        "2. Warrior\n3. Gunner\n4. Archer\n").title() 
     if(Role.lower() in (Roles)): 
      return 
     else: 
      print("That's not a role! Try again.") 
self = choose_name() 
self = choose_role() 

Nach dem Hauptcode der Results (wie im Titel) oder das Ergebnis Code sollte die Stats entsprechend der Reihenfolge der Instanzen print aus. Hier ist das Ergebnis 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)) 

Wenn ich den Code ausführen bekomme ich einen Fehler:

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

Ich glaube, ich habe Ideen, warum dies geschieht, aber ich weiß nicht, wie man es beheben . Können Sie mir helfen, wo ich mit meinem Code anfangen muss und wie ich ihn beheben kann? Hier die Ergebnisse I (in diesem Beispiel der Name = Bob und Role = Gunner) wollte:

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

Ihr Fehler ist auf der Linie 50, aber der Code Sie legen nur 36 Zeilen. Auf jeden Fall in Ihrem Stat beheben beide (Ihre Großschreibung wurde durcheinander gebracht). Sie sollten 'self.name = name' und' self.role = role' verwenden. – Keatinge

+0

@Keatinge Wo gebe ich diese hin? Ich nehme an, du meinst außerhalb der Klasse, aber ich weiß es wirklich nicht. – BradTheBrutalitist

+2

@BradTheBrutalist. Innerhalb der Stat-Klasse haben Sie die Zeile 'self.name = Name' ersetzen Sie sie mit' self.name = name' und ebenfalls für self.role. ** Python ist case sensitive ** – Keatinge

Antwort

2

Ich bin ein wenig zögerlich, dies zu schreiben, weil der Code ein Riesendurcheinander ist. Aber du hast gesagt, du wolltest kein Neuschreiben, du wolltest nur deinen Code sehen, also hier ist es behoben.Ich habe kommentiert alles, was ich geändert

#renamed stat to player, the point of the object shouldn't be to hold just the stats, should hold all information 
#pertaining to the player/dicer 
class Dicer(): 
    #levelm, totalexp, health removed from constructor 
    def __init__(self, name, role, health, attack, defense): 
     self.name = name #changed Name to name 
     self.level = 1 
     self.totexp = 0 
     self.role = role #changed Role to role 
     self.health = health 
     self.attack = attack 
     self.defense = defense 
     self.exp = 0 

    #instance function to print the stats of the dicer 
    def print_info(self): 
     print(
      "Here is your Dicer's information:\nDicer's name: {0}\nLevel: {1}\nExperience: {2}\nRole:"\ 
      " {3}\nRole Stats:\nHealth: {4}\nAttack: {5}\nDefense: {6}".format(self.name, self.level, self.totexp, self.role, self.health, self.attack, self.defense) 
     ) 
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: 
      #you have to return the name so you can use it later 
      return Name 

def choose_role(): 
    #removed all the lambdas here 
    Roles = { 
    'mage': (100, 40, 10), #(Health, Attack, Defense) 
    'warrior': (100, 30, 20), 
    'gunner': (100, 20, 30), 
    'archer': (100, 10, 40) 
    } 

    while(True): 
     Role = input("What role do you want to be?\nChoices:\n1. Mage\n2. Warrior\n3. Gunner\n4. Archer\n").title() 
     if(Role.lower() in (Roles)): 
      #don't just return None, return the name of the role and it's 3-tuple stats 
      #returning the role and it's stats from a function is not the best way to do this 
      return Role, Roles[Role.lower()] 
     else: 
      print("That's not a role! Try again.") 

dicer_name = choose_name() 
dicer_role_name, dicer_role_stats = choose_role() 

#create a dicer using the stats returned from chose_role() function 
mainDicer = Dicer(dicer_name, dicer_role_name, dicer_role_stats[0], dicer_role_stats[1], dicer_role_stats[2]) 
mainDicer.print_info() 

Hier ist, wie ich es tun würde, wenn es von Grund auf neu zu schreiben

class Dicer(): 
    def __init__(self, name, role, statTuple): 
     self.name = name 
     self.level = 1 
     self.xp = 0 
     self.health = statTuple[0] 
     self.attack = statTuple[1] 
     self.defense = statTuple[2] 

    def print_stats(self): 
     print("Name", self.name) 
     print("Level", self.level) 
     print("XP", self.xp) 
     print("Health", self.health) 
     print("Attack", self.attack) 
     print("Defense", self.defense) 


roles = { 
    #health, attack defense 
    "mage" : (100, 40, 10), 
    "warrior" : (100, 30, 20), 
    "gunner" : (100, 20, 30), 
    "archer" : (100, 10, 40) 
} 
name = input("What is your name ") 
dicer_role_choice = None 
while dicer_role_choice not in roles.keys(): 
    print("Please select a dicer:") 

    #prints each role on a new line, showing all the possible choices 
    print("\n".join(roles.keys())) 
    dicer_role_choice = input() 

mainDicer = Dicer(name, dicer_role_choice, roles[dicer_role_choice]) 
mainDicer.print_stats() 
+0

Ich bin überrascht, dass der Code nicht neu gemacht werden musste. Ich bin froh, dass du neu bist, was falsch war. – BradTheBrutalitist

+0

@BradTheBrutalitist Ich meine, es sollte wirklich neu gemacht werden.Du hast die ganze Logik geschrieben und ich habe nur die ganze Sache zusammengeschnitten. – Keatinge

+0

Ja, ich bin so schlecht mit 'Klasse' Ich mache normalerweise nur def() Funktionen, weil ich sie am meisten kenne. – BradTheBrutalitist

0

ich meinen Code hier neu gestaltet ist:

-Code-Anbieter: Bradley Elko

Update 1 [51 Zeilen]

Update 2 [97 Zeilen]

Update 3 [127 Zeilen]

Update 4 [148 Zeilen]


class Char(): 
    def __init__(self): 
     self.name = "" 
     self.role = "" 
     self.health = 0 
     self.health_now = 0 
     self.health_gained = 0 
     self.attack = 0 
     self.defense = 0 
     self.glob() 
     self.add_name() 
     self.add_role() 
     self.print_info() 
     self.menu() 
    def glob(self): 
     global Level, TotXp, Xp, Info, Points 
     Level = 1 
     Points = 15 
     TotXp = 0 
     Xp = 0 
     Info = "Not Filled In Yet" 
    def print_info(self): 
     print("\nYour Character's Stats:") 
     print("Your character's name is: {0}".format(self.name)) 
     print("Your character is level: {0}".format(Level)) 
     print("You have {0} Level Points\n".format(Points)) 
     print("Your Role Stats:") 
     print("Your character's role is: {0}\nYour character's health is: {1}\nYour character's attack is: {2}\nYour character's defense is: {3}".format(self.role, self.health, self.attack, self.defense)) 
     print("\nIntroduction To Game:\n{0}".format(Info)) 
    def add_name(self): 
     while(True): 
      Name = input("What's your character's name?\n") 
      if(len(Name) < 3): 
       print("That name is too short!") 
      elif(len(Name) > 20): 
       print("That name is too long!") 
      elif(any(bad_word in Name.lower() for bad_word in ['fuck'])): 
       print("That name is inappropriate!") 
      else: 
       self.name += Name 
       break 
    def add_role(self): 
     global Role 
     Roles = { 
     "Marksman": (100, 50, 30), 
     "Melee":(100, 40, 40), 
     "Mage":(100, 30, 50) 
     } 
     while(True): 
      Role = input("\nWhat's your characters role?\nChoices:\n1. Marksman\n2. Melee\n3. Mage\n").title() 
      if(Role not in ['Marksman', 'Melee', 'Mage']): 
       print("That's not a role!") 
      else: 
       self.role += Role 
       self.health += Roles[Role][0] 
       self.health_now += Roles[Role][0] 
       self.attack += Roles[Role][1] 
       self.defense += Roles[Role][2] 
       break 
    def menu(self): 
     global Choice 
     print("\nMenu\nWelcome to the menu! Here is where you will be staying in between fights, regaining health, and using the shop.\n") 
     while(True): 
      Choice = input("Where do you want to go?\n1. Shop\n2. Fight\n3. Sleep\n4. Role Stats\n5. Char Stats\n").lower() 
      if(Choice == "shop"): 
       return self.shop() 
      elif(Choice == "fight"): 
       print("Needing updating!") 
       break 
      elif(Choice == "sleep"): 
       self.sleeping() 
       break 
      elif(Choice == "role stats"): 
       return self.role_stats() 
      elif(Choice == "char stats"): 
       return self.char_stats() 
      else: 
       print("That's not an option!") 
    def menu_two(self): 
     global Choice 
     print("\nMenu\nWelcome back to the menu!") 
     while(True): 
      Choice = input("Where do you want to go next?\n1. Shop\n2. Fight\n3. Sleep\n4. Role Stats\n5. Char Stats\n").lower() 
      if(Choice == "shop"): 
       return self.shop() 
      elif(Choice == "fight"): 
       print("Needing updating!") 
       break 
      elif(Choice == "sleep"): 
       self.sleeping() 
       break 
      elif(Choice == "role stats"): 
       return self.role_stats() 
      elif(Choice == "char stats"): 
       return self.char_stats() 
      else: 
       print("That's not an option!") 
    def role_stats(self): 
     global Choice 
     if(Choice == "role stats"): 
      print("\nRole Stats:\nRole: {0}\nHealth: {1}\nAttack: {2}\nDefense: {3}\n".format(self.role, self.health, self.attack, self.defense)) 
      return self.menu_two() 
    def shop(self): 
     while(True): 
      Buy = input("\nWelcome to the shop! Here is were you spend your level points. What do you want to purchase?\nChoices:\n1. Abilities\n2. Weapons\n3. Stat Points\n4. Nothing\n").lower() 
      if(Buy == "abilities"): 
       print("\nYou selected 'Abilities'.") 
       break 
      elif(Buy == "weapon"): 
       print("\nYou selected 'Weapons'.") 
       break 
      elif(Buy == "stat points"): 
       print("\nYou selected 'Stat Points'.") 
       break 
      elif(Buy == "nothing"): 
       print("\nReturning to menu...") 
       return self.menu_two() 
      else: 
       print("\nThat is not an option!") 
    def char_stats(self): 
     global Choice 
     if(Choice == "char stats"): 
      print("\nYour Character's Stats:") 
      print("Username: {0}".format(self.name)) 
      print("Level: {0}".format(Level)) 
      print("Level Points: {0}".format(Points)) 
      return self.menu_two() 
    def sleeping(self): 
     global Choice 
     import time 
     if(Choice == "sleep" and self.health_now >= self.health): 
      print("\nYour health is full and so you don't need to sleep. Returning to menu.") 
      return self.menu_two() 
     elif(Choice =="sleep" and self.health_now < self.health): 
      print("\nYou are sleeping to regain health. This doubles your characters health regeneration! Please wait patently as your character sleeps to regain health faster.") 
      for i in range(1, 1801): 
       time.sleep(1) 
       if(i in [60,120,180,240,300,360,420,480,540,600,660,720,780,840,900,960,1020,1080,1140,1200,1260,1320,1380,1440,1500,1560,1620,1680,1740,1800]): 
        self.health_gained += 10 
        self.health_now += 10 
        if(self.health_now < self.health): 
         print("\nYou're still healing. You've regained %d health!\nCurrent health:\nHealth: %d/%d" % (self.health_gained, self.health_now, self.health)) 
        else: 
         print("\nYou're done healing! You've regained %d health!\nCurrent health:\nHealth: %d/%d" % (self.health_gained - (self.health_now - self.health), self.health, self.health)) 
         print("\nYou woke up from your healing nap. Returning to menu.") 
         return self.menu_two() 
        self.health_gained -= 10 
MyChar = Char() 

Update 1 Datum [2016.06.10]

Update 2 Datum [6/10/2016]

Update 3 Datum [6/11/2016]

Update 4 Datum [2016.06.14]

Verbindung 1: https://repl.it/C0gh/46

Link 2: https://repl.it/C0gh/60

Link 3: https://repl.it/C0gh/79