2017-07-04 6 views
0

Ich habe den folgenden Code:Python Formatierung der Ausgabe

import options 
import random 


class Player(): 
    def __init__(self): 
     self.name = None 
     self.gold = 100 
     self.maxhealth = 100 
     self.health = self.maxhealth 
     self.level = 1 
     self.exp = 0 
     self.levelUp = 50 
     self.gainedexp = self.levelUp - self.exp 


    def get_name(self): 
     self.name = input("Hey there, traveller! What's your name?\n~~>") 
     print("Since you are new around here, 100 gold doubloons have been given to you, {}!".format(self.name)) 

    def gold_counter(self): 
     print("You currently have {} gold!".format(player.gold)) 


class Dragon(): 
    def __init__(self): 
     self.name = "Dragon" 
     self.dropgold = random.randint(13,20) 
     self.minexp = int(15 * round(player.level * 1.5)) 
     self.maxexp = int(30 * round(player.level * 1.5)) 
     self.expgain = random.randint({}, {}.format(self.minexp, self.maxexp)) 
     self.maxhealth = 80 
     self.health = self.maxhealth 




def intro(): 
    wrong_input = 0 
    nar_name = "Narrator" 
    print("{}: Uhhhm...".format(nar_name)) 
    print("{}: Let me check my list...".format(nar_name)) 
    print("{0}: Ah! Yes! {1}, that's right. I heard you were supposed to be arriving today.".format(nar_name, player.name)) 

ich auch noch zwei weitere Module verwenden, aber ich bin 99% sicher, dass sie dies tun nicht beeinflussen. Ich erhalte die folgende Ausgabe:

Hey there, traveller! What's your name? 
~~>Savage Potato 
Since you are new around here, 100 gold doubloons have been given to you, Savage Potato! 
Do you want to see your balance? 
~~> Yes 
You currently have 100 gold. 
Narrator: Uhhhm... 
Narrator: Let me check my list... 
Narrator: Ah! Yes! None, that's right. I heard you were supposed to be arriving today. 

In der letzten Zeile, wird der Name des Narrator ausdrucken, aber nicht den eingegebenen Namen des Benutzers. Ich habe auch die Python-Dokumente unter their website angeschaut, aber ich konnte keine Lösung finden. Irgendwelche Ideen, wie ich verhindern könnte, dass None als der Name des Benutzers ausgegeben wird?

EDIT # 1: Ich habe Player = Player() später im Modul geschrieben.

EDIT # 2: Dies ist der gesamte Code I verwendet:

Modul 1 (main.py)

import prints 
import random 

class Player(): 
    def __init__(self): 
     self.name = None 
     self.gold = 100 
     self.maxhealth = 100 
     self.health = self.maxhealth 
     self.level = 1 
     self.exp = 0 
     self.levelUp = 50 
     self.gainedexp = self.levelUp - self.exp 


    def get_name(self): 
     self.name = input("Hey there, traveller! What's your name?\n~~>") 
     print("Since you are new around here, 100 gold doubloons have been given to you, {}!".format(self.name)) 




class Dragon(): 
    def __init__(self): 
     self.name = "Dragon" 
     self.dropgold = random.randint(13,20) 
     self.minexp = int(15 * round(player.level * 1.5)) 
     self.maxexp = int(30 * round(player.level * 1.5)) 
     self.expgain = random.randint({}, {}.format(self.minexp, self.maxexp)) 
     self.maxhealth = 80 
     self.health = self.maxhealth 




#while player.exp >= player.levelUp: 
    #player.levelUp += 1 
    #player.exp = player.exp - player.levelUp 
    #player.levelUp = round(player.levelUp * 1.5) 
    #print("Congrats! You just levelled up to level {} by gaining {} experience!".format(player.level, player.gainedexp)) 


def start(): 
    player.get_name() 
    prints.gold_counter() 
    prints.intro() 
    prints.encounter() 





player = Player()  


start() 

Modul 2 (prints.py)

import options 
import random 


class Player(): 
    def __init__(self): 
     self.name = None 
     self.gold = 100 
     self.maxhealth = 100 
     self.health = self.maxhealth 
     self.level = 1 
     self.exp = 0 
     self.levelUp = 50 
     self.gainedexp = self.levelUp - self.exp 


    def get_name(self): 
     self.name = input("Hey there, traveller! What's your name?\n~~>") 
     print("Since you are new around here, 100 gold doubloons have been given to you, {}!".format(self.name)) 

    def gold_counter(self): 
     print("You currently have {} gold!".format(player.gold)) 


class Dragon(): 
    def __init__(self): 
     self.name = "Dragon" 
     self.dropgold = random.randint(13,20) 
     self.minexp = int(15 * round(player.level * 1.5)) 
     self.maxexp = int(30 * round(player.level * 1.5)) 
     self.expgain = random.randint({}, {}.format(self.minexp, self.maxexp)) 
     self.maxhealth = 80 
     self.health = self.maxhealth 




def intro(): 
    wrong_input = 0 
    nar_name = "Narrator" 
    print("{}: Uhhhm...".format(nar_name)) 
    print("{}: Let me check my list...".format(nar_name)) 
    print("{0}: Ah! Yes! {1}, that's right. I heard you were supposed to be arriving today.".format(nar_name, player.name)) 
    print("{}: Welcome to... THE DRAGON FIGHTER GAME!".format(nar_name)) 
    print("{}: I know, it isn't the most imaginative name.".format(nar_name)) 
    print("{}: Don't look at me like that, I tried my hardest!".format(nar_name)) 
    print("{}: Anyhoo, let's carry on.".format(nar_name)) 
    print("{}: For some stupid reason, the creator of this game didn't give me an actual name, so\nmy name is just \"Narrator\" or \"N\", but you can call me Larry.".format(nar_name)) 
    while True: 
     option = input("Narrator: Actually, which name would you prefer to call me?\n").upper() 
     if option in options.nar_larry_opt: 
      nar_name = "Larry" 
     elif option in options.nar_narrator_opt: 
      nar_name = "Narrator" 
      while True: 
       ask = input("{}: Maybe \"N\" for short?".format(nar_name)).upper() 
       if ask in options.inp_yes_opt: 
        nar_name = "N" 
       elif ask in options.inp_no_opt: 
        break 
       else: 
        wrong_input += 1 
        if wrong_input == 1: 
         print("Please try again.") 
        elif wrong_input == 2: 
         print("Try to not put the same thing in next time.") 
        elif wrong_input == 3: 
         print("This isn't funny.") 
        elif wrong_input == 4: 
         print("Seriously.") 
        elif wrong_input == 5: 
         print("OKAY! THIS IS IT! GO BACK TO THE BEGINNING!") 
         intro() 

        continue 
       break 
     else: 
      print("Please try again.") 
      continue 
     break 
    print("{}: So, as I was saying, this game is basically just some dragon quest thingy.".format(nar_name)) 
    print("{}: You'll probably get tips from me every now and again if I can be bothered.".format(nar_name)) 
    print("{}: I'll get an test encounter ready.".format(nar_name)) 



def gold_counter(): 
    while True: 
     option = input("Do you want to see your balance?\n~~> ").upper() 
     if option in options.inp_yes_opt: 
      print("You currently have {} gold.".format(player.gold)) 
     elif option in options.inp_no_opt: 
      print("You can check your balance later in the game.") 
     else: 
      print("Please try again.") 
      continue 
     break 



def encounter(): 
    while True: 
     dragon_appear = random.randint(1,2) 
     if dragon_appear == 1: 
      print("What's that? Looks like a huge bir... \nA DRAGON! A MAJESTIC DRAGON JUST FLEW DOWN FROM THE SKY!") 
     else: 
      print("What's that? Looks like a huge bir... \n Yeah. Just a giganta-bird.") 

    while encounter().dragon_appear != 2: 
     print("So that's the message you'll get when a dragon appears.") 
     print("And you will be prompted whether you want to run or fight, like so:") 
     while True: 
      wrong_input = 0 
      ask = input("Run away like a coward, or fight the majestic beast?") 
      if ask in options.enc_run_opt: 
       escape = random.randint(1,2) 
       if escape == 1: 
        print("You managed to get away!") 
       else: 
        print("You didn't get away. Better luck next time!") 
      elif ask in options.enc_attack_opt: 
       pass 
      else: 
       wrong_input += 1 
       if wrong_input == 1: 
        print("Please try again.") 
       elif wrong_input == 2: 
        print("Try to not put the same thing in next time.") 
       elif wrong_input == 3: 
        print("This isn't funny.") 
       elif wrong_input == 4: 
        print("Seriously.") 
       continue 
      break 




player = Player() 

Modul 3 (options.py)

inp_yes_opt = {"Y", "YE", "YES", "YEAH", "PLEASE", "YES PLEASE"} 
inp_no_opt = {"N", "NO", "NOPE", "NAH"} 

nar_larry_opt = {"LARRY", "LARR", "LAR", "LA", "L", "LARRY PLEASE"} 
nar_narrator_opt = {"NARRATOR", "NARR", "N", "NAR", "NARRATE", "NOT LARRY"} 

enc_run_opt = {"RUN", "RU", "R", "SCRAM", "RUN AWAY", "RUUUUN"} 
enc_attack_opt = {"ATTACK", "ATTAK", "A", "FIGHT", "F", "ATTACK", ""} 
+0

Es scheint, Sie Verwenden Sie nicht die gleiche Instanz von Player, versuchen Sie, es in Intro-Funktion zu erstellen, sollte es funktionieren –

+0

Ich werde es versuchen, und ich werde zurück zu Ihnen. Ich dachte nur, es wäre einfacher gewesen, es einfach in eine Klasse zu verwandeln und es dann anzurufen, wenn es nötig wäre. Es macht einfach mehr Sinn, es in der Klasse zu haben. – SavagePotato

+1

Sie sollten die Sichtbarkeit Ihres Objekts sehen. Wenn es global ist, funktioniert es, weil jede Funktion Zugriff darauf hat. Wenn es jedoch lokal ist, zum Beispiel wenn Sie es in eine Funktion oder in eine andere Klasse einfügen, wird es die Änderungen nicht speichern, weil Sie auf ein anderes Objekt verweisen. –

Antwort

3

Wenn Sie o. drucken möchten Wenn Sie den Namen des Players eingeben, müssen Sie das Player-Objekt als Parameter an die Intro-Funktion übergeben. Das setzt voraus, Intro ist nicht das Spielerobjekt zu erfassen und das Spielerobjekt ist nicht global

Im Moment scheint es, gibt es keinen Spieler Objekt zugänglich für den Umfang der Funktion, weshalb es keine gibt

+0

Habe es gerade bearbeitet. Über etwas vergessen. Mein Fehler. – SavagePotato

+0

Können Sie den Code teilen, der die Intro-Funktion aufruft? –

+0

Ich stelle mir vor, wenn diese Funktion ganz oben definiert ist, ist es nicht in der Lage, die Player-Instanz zu erfassen, wie Sie beabsichtigen, also sollten Sie Player als Parameter übergeben und es wird Ihr Problem lösen –