2016-04-26 11 views
-1

Ich habe Schwierigkeiten beim Versuch, diesen Fehler zu beheben:Python und globale Deklaration

SyntaxWarning: name 'meaning5' is assigned to before global declaration

Grundsätzlich mein Programm den Benutzer zur Eingabe ihres Namens ermöglichen muss, berechnet das Programm dann die Benutzer Glückszahl basierend auf die Zuordnung von a = 1, b = 2 usw.

Dies ist mein Code so weit:

from time import sleep 

tempNumb = 0 
tempLNN1a = 0 
tempLNN1b = 0 
tempLNN2a = 0 
tempLNN2b = 0 
varLNN1 = 0 
varLNN2 = 0 
LNN = 0 
tempLNNa = 0 
tempLNNb = 0 
templetter = "nothing" 
meaning1 = "Natural leader" 
meaning2 = "Natural peacemaker" 
meaning3 = "Creative and optimistic" 
meaning4 = "Hard worker" 
meaning5 = "Value freedom" 
meaning6 = "Carer and a provider" 
meaning7 = "Thinker" 
meaning8 = "Have diplomatic skills" 
meaning9 = "Selfless and generous" 
global templetter 
global tempNumb 
global tempLNN1a 
global tempLNN1b 
global tempLNN2a 
global tempLNN2b 
global varLNN1 
global varLNN1 
global LNN 
global tempLNNa 
global tempLNNb 
global meaning1 
global meaning2 
global meaning3 
global meaning4 
global meaning5 
global meaning6 
global meaning7 
global meaning8 
global meaning9 


def mainprogram(): 
    sleep(1) 
    print("-----------------------") 
    print("Welcome to LUCKY NAME \n   NUMBERS") 
    print("-----------------------") 
    sleep(1) 
    firstname = input("Please input your first \nname in all capitals\n") 
    if firstname == firstname.upper(): 
     print("-----------------------") 
     sleep(1) 
     surname = input("Please input your surname \nin all capitals\n") 
     if surname == surname.upper(): 
      print("-----------------------") 
      print("Calculating your Lucky \nName Number...") 
      for i in range(len(firstname)): 
       templetter = firstname[i] 
       calculate() 
       tempfirstname() 
      for i in range(len(surname)): 
       templetter = surname[i] 
       calculate() 
       tempsurname() 
      finalcalculate() 


def calculate(): 
    if templetter == "A": 
     tempNumb = 1 
    elif templetter == "B": 
     tempNumb = 2 
    elif templetter == "C": 
     tempNumb = 3 
    elif templetter == "D": 
     tempNumb = 4 
    elif templetter == "E": 
     tempNumb = 5 
    elif templetter == "F": 
     tempNumb = 6 
    elif templetter == "G": 
     tempNumb = 7 
    elif templetter == "H": 
     tempNumb = 8 
    elif templetter == "I": 
     tempNumb = 9 
    elif templetter == "J": 
     tempNumb = 1 
    elif templetter == "K": 
     tempNumb = 2 
    elif templetter == "L": 
     tempNumb = 3 
    elif templetter == "M": 
     tempNumb = 4 
    elif templetter == "N": 
     tempNumb = 5 
    elif templetter == "O": 
     tempNumb = 6 
    elif templetter == "P": 
     tempNumb = 7 
    elif templetter == "Q": 
     tempNumb = 8 
    elif templetter == "R": 
     tempNumb = 9 
    elif templetter == "S": 
     tempNumb = 1 
    elif templetter == "T": 
     tempNumb = 2 
    elif templetter == "U": 
     tempNumb = 3 
    elif templetter == "V": 
     tempNumb = 4 
    elif templetter == "W": 
     tempNumb = 5 
    elif templetter == "X": 
     tempNumb = 6 
    elif templetter == "Y": 
     tempNumb = 7 
    elif templetter == "Z": 
     tempNumb = 8 
    else: 
     "You managed to break it." 
     mainprogram() 

def tempfirstname(): 
    varLNN1 = varLNN1 + tempNumb 

def tempsurname(): 
    varLNN2 = varLNN2 + tempNumb 

def finalcalculate(): 
    varLNN1 = str(varLNN1) 
    varLNN2 = str(varLNN2) 
    tempLNN1a = varLNN1[0] 
    tempLNN1b = varLNN1[1] 
    tempLNN2a = varLNN2[0] 
    tempLNN2b = varLNN2[1] 
    varLNN1 = int(tempLNN1a) + int(tempLNN1b) 
    varLNN2 = int(tempLNN2a) + int(tempLNN2b) 
    LNN = varLNN1 + varLNN2 
    LNN = str(LNN) 
    tempLNNa = LNN[0] 
    tempLNNb = LNN[1] 
    LNN = int(tempLNNa) + int(tempLNNb) 
    if LNN == 1 or "1": 
     print("Your Lucky Name Number is - " + str(LNN) + " and it means you are a " + meaning1) 
     loop() 
    elif LNN == 2 or "2": 
     print("Your Lucky Name Number is - " + str(LNN) + " and it means you are a " + meaning2) 
     loop() 
    elif LNN == 3 or "3": 
     print("Your Lucky Name Number is - " + str(LNN) + " and it means you are " + meaning3) 
     loop() 
    elif LNN == 4 or "4": 
     print("Your Lucky Name Number is - " + str(LNN) + " and it means you are a " + meaning4) 
     loop() 
    elif LNN == 5 or "5": 
     print("Your Lucky Name Number is - " + str(LNN) + " and it means you " + meaning5) 
     loop() 
    elif LNN == 6 or "6": 
     print("Your Lucky Name Number is - " + str(LNN) + " and it means you are a " + meaning6) 
     loop() 
    elif LNN == 7 or "7": 
     print("Your Lucky Name Number is - " + str(LNN) + " and it means you are a " + meaning7) 
     loop() 
    elif LNN == 8 or "8": 
     print("Your Lucky Name Number is - " + str(LNN) + " and it means you " + meaning8) 
     loop() 
    elif LNN == 9 or "9": 
     print("Your Lucky Name Number is - " + str(LNN) + " and it means you are " + meaning9) 
     loop() 
    else: 
     print("Somehow your lucky name number is too high...") 
     mainprogram() 
+1

Es sollte sein 'wenn LNN == 1 oder LNN ==" 1 ":' und so weiter, weil 'if" 1 "' oder 'if" 6 "' oder sogar 'if" string "' immer zurück ' Wahr '. – linusg

+2

Bitte lesen Sie über Listen, da sie Ihren Code viel besser und effizienter machen. Versuchen Sie: http://www.tutorialspoint.com/python/python_lists.htm – Oisin

Antwort

2

Python ist anders als C, Sie müssen keine globale Variable deklarieren, wenn Sie eine globale Variable in einer Funktion verwenden, müssen Sie das globale Schlüsselwort verwenden. Zum Beispiel:

 meaning5 = "Value freedom" 
    def somefunction(): 
     global meaning5 
     print meaning5 

und als linsug Verwendung Listen gesagt hat.

0

einfach alle die globale Variable in einer einzigen Funktion wie definieren:

def global_variables(): 
    global var_1 
    global var_2 
    global var_3 
    global var_4 
    . 
    global var_n 

Und vor dem ersten irgendetwas anderes in Haupt, rufen Sie diese Funktion aufrufen:

if __name__ == "__main__": 
    global_variables() 
    # Rest of your code. 

Könnte es helfen !. Danke