我在尝试解决此错误时遇到问题:
SyntaxWarning: name 'meaning5' is assigned to before global declaration
基本上我的程序需要允许用户输入他们的名字,程序然后根据a = 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()
答案 0 :(得分:2)
Python与C不同,您不必声明变量global,如果您在函数中使用全局变量,则需要使用global关键字。 例如:
meaning5 = "Value freedom"
def somefunction():
global meaning5
print meaning5
正如linsug所说的使用清单。
答案 1 :(得分:0)
只需在单个函数中定义所有全局变量,如:
def global_variables():
global var_1
global var_2
global var_3
global var_4
.
global var_n
在调用main中的任何其他内容之前,先调用此函数:
if __name__ == "__main__":
global_variables()
# Rest of your code.
可能有帮助!感谢名单