我的代码:全局名称没有被定义的错误
import sys
import time
import random
def main():
print('***TEST**** Grad School Multiplier=',gradschoolmultiplier,'***TEST***')
x=gradschoolmultiplier*50000
print('Your salary in dollars, $',x)
def start():
gradschool=input('Do you intend to go to Graduate School? ')
print('')
time.sleep(2)
if gradschool=='yes':print('That is a fantastic if expensive decision.')
elif gradschool=='Yes':print('That is a fantastic if expensive decision.')
elif gradschool=='Y':print('That is a fantastic if expensive decision.')
elif gradschool=='y':print('That is a fantastic if expensive decision.')
elif gradschool=='YES':print('That is a fantastic if expensive decision.')
else:print('No? Well, then it\'s off to work to pay back those student loans.')
print('')
if gradschool=='yes':g1=3
elif gradschool=='Yes':g1=3
elif gradschool=='Y':g1=3
elif gradschool=='y':g1=3
elif gradschool=='YES':g1=3
else:g1=1
g=random.randrange(1, 3)
if g==1:gradschoolmultiplier=1
else:gradschoolmultiplier=g1*g/2
time.sleep(2)
main()
start()
当然,我得到:
NameError: global name 'gradschoolmultiplier' is not defined
我不是足够聪明,了解这个问题的答案为别人着想。有人会用傻瓜的话来解释答案吗?谢谢!
尝试'如果gradschool.lower()的( “Y”, “是”)',把在在每个':'之后换行,然后将'print'和'g1'赋值到同一个块中。 – jonrsharpe
http://www.python.org/dev/peps/pep-0008/ - 你真的需要阅读和理解这个文件。此外,如果您多次重复完全相同的代码,则会造成错误(请参阅@ jonrsharpe的评论)。有一个'main'方法,但使用'start()'作为“主要”方法也是一个坏主意...... – ThiefMaster