2016-04-26 45 views
-1

私はこのエラーを解決しようとするとトラブルを抱えている:Pythonとグローバル宣言

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

は基本的に私のプログラムは、入力に自分の名前をユーザーに許可する必要があり、その後、プログラムは上基づいてユーザーラッキーナンバーを算出し、 = 1、B = 2などの割り当て

は、これはこれまでのところ私のコードです:

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

LNN == 1またはLNN == "1": 'のようなものでなければなりません。' '1 ''または '' if "6" 'や' 'if"真」。 – linusg

+2

あなたのコードをより良く、より効率的にするため、リストをお読みください。試してみてください:http://www.tutorialspoint.com/python/python_lists.htm – Oisin

答えて

2

PythonはCとは異なり、変数をグローバルに宣言する必要はありません。関数内でグローバル変数を使用する場合は、globalキーワードを使用する必要があります。 例:

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

と同様に、linsugには使用リストがあります。

0

だけのような単一の関数内のすべてのグローバル変数を定義します。

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

そしてメインで何かを呼び出す前に、まずこの関数を呼び出す:

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

それが役立つかもしれません! Thanx

関連する問題