我面臨着一個關於全局變量的問題,儘管它是python聲明並不違抗。實質上,我只想檢查整數是否包含小數位,或者輸入中是否包含任何與整數無關的整數。這裏是我的代碼:全局名稱未定義?同性戀夥伴們:
def Strength1():
try:
global strength1
strength1 = int(input("%s, please enter your desired strength - between 1 and 20\n>"%name1))
strength1int = int(strength1)
def invLoop():
clearScreen()
Invalid()
Strength1()
if int(strength1) <= 0:
invLoop()
if int(strength1) >= 21:
invLoop()
except Exception as exception:
clearScreen()
print("'%s isn't an integer."%strength1)
Strength1()
def Skill1():
try:
global skill1
skill1 = int(input("%s, please enter your desired skill - between 1 and 20\n>"%name1))
skill1int = int(skill1)
def invLoop():
clearScreen()
Invalid()
Skill1()
if int(skill1) <= 0:
invLoop()
if int(skill1) >= 21:
invLoop()
except Exception as exception:
clearScreen()
print("'%s isn't an integer."%skill1)
Skill1()
def Strength2():
try:
global strength2
strength2 = int(input("%s, please enter your desired strength - between 1 and 20\n>"%name2))
def invLoop():
clearScreen()
Invalid()
Strength2()
if int(strength2) <= 0:
invLoop()
if int(strength2) >= 21:
invLoop()
except Exception as exception:
clearScreen()
print("'%s' isn't an integer."%strength2)
Strength2()
def Skill2():
try:
global skill2
skill2 = int(input("%s, please enter your desired skill - between 1 and 20\n>"%name2))
def invLoop():
clearScreen()
Invalid()
Skill2()
if int(skill2) <= 0:
invLoop()
if int(skill2) >= 21:
invLoop()
except Exception as exception:
clearScreen()
print("'%s' isn't an integer."%skill2)
Skill2()
,這是我的錯誤:
Traceback (most recent call last):
File "H:\Toby Reichelt\A453\Two Encounters - Developing Dice.py", line 29, in Skill1
skill1 = int(input("%s, please enter your desired skill - between 1 and 20\n>"%name1))
ValueError: invalid literal for int() with base 10: '0.5'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "H:\Toby Reichelt\A453\Two Encounters - Developing Dice.py", line 197, in <module>
mainloop()
File "H:\Toby Reichelt\A453\Two Encounters - Developing Dice.py", line 188, in mainloop
Skill1()
File "H:\Toby Reichelt\A453\Two Encounters - Developing Dice.py", line 41, in Skill1
print("'%s isn't an integer."%skill1)
NameError: global name 'skill1' is not defined