全局名称不是定义错误。

时间:2022-07-14 18:14:19

My code:

我的代码:

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()

And of course I get:

当然,我得到:

NameError: global name 'gradschoolmultiplier' is not defined

I am not smart enough to understand the answers to this question for others. Would someone be so kind as to explain the answer in simpletons' terms? Thanks!

我不够聪明,不能替别人理解这个问题的答案。有人会好心地用傻瓜的语言解释这个答案吗?谢谢!

2 个解决方案

#1


2  

Indeed as @Dan says, the scoping problem.

正如@Dan所说的,范围问题。

Or you can use global variables.

或者你可以使用全局变量。

Some of my other suggestions on your code:

我对您的代码的一些其他建议:

import sys
import time
import random


def print_salary():
    print('***TEST**** Grad School Multiplier=',gradschoolmultiplier,'***TEST***')
    x = gradschoolmultiplier*50000
    print('Your salary in dollars, $',x)

def main():    
    gradschool=input('Do you intend to go to Graduate School? ')
    print('')
    time.sleep(2)

    if gradschool.lower() in {'yes', 'y'}:
        print('That is a fantastic if expensive decision.')
        g1 = 3
    else:
        print('No?  Well, then it\'s off to work to pay back those student loans.')
        g1 = 1
    print('')

    g = random.randrange(1, 3)
    global gradschoolmultiplier
    if g == 1:
        gradschoolmultiplier = 1
    else:
        gradschoolmultiplier = g1 * g / 2
    time.sleep(2)
    print_salary()

if __name__ == '__main__':
    main()

You should combine some the if statements to make it simpler.

你应该结合一些if语句来使它更简单。

Oh, we share the same thoughts @jonrsharpe

哦,我们有相同的想法@jonrsharpe。

Quick improvement as suggested by @Nils

@Nils建议快速改进

#2


1  

gradschoolmultiplier is not in the scope of main(), it only exists in start().

小学乘数不在main()的范围内,它只存在于start()中。

You can pass it into main.

你可以把它传递给main。

change the call to main to be main(gradschoolmultiplier)

把呼叫中心改为主要的(小学乘法器)

change def main() to def main(gradschoolmultiplier):

将def main()更改为def main(小学乘数):

Information on python scoping

python的信息范围

#1


2  

Indeed as @Dan says, the scoping problem.

正如@Dan所说的,范围问题。

Or you can use global variables.

或者你可以使用全局变量。

Some of my other suggestions on your code:

我对您的代码的一些其他建议:

import sys
import time
import random


def print_salary():
    print('***TEST**** Grad School Multiplier=',gradschoolmultiplier,'***TEST***')
    x = gradschoolmultiplier*50000
    print('Your salary in dollars, $',x)

def main():    
    gradschool=input('Do you intend to go to Graduate School? ')
    print('')
    time.sleep(2)

    if gradschool.lower() in {'yes', 'y'}:
        print('That is a fantastic if expensive decision.')
        g1 = 3
    else:
        print('No?  Well, then it\'s off to work to pay back those student loans.')
        g1 = 1
    print('')

    g = random.randrange(1, 3)
    global gradschoolmultiplier
    if g == 1:
        gradschoolmultiplier = 1
    else:
        gradschoolmultiplier = g1 * g / 2
    time.sleep(2)
    print_salary()

if __name__ == '__main__':
    main()

You should combine some the if statements to make it simpler.

你应该结合一些if语句来使它更简单。

Oh, we share the same thoughts @jonrsharpe

哦,我们有相同的想法@jonrsharpe。

Quick improvement as suggested by @Nils

@Nils建议快速改进

#2


1  

gradschoolmultiplier is not in the scope of main(), it only exists in start().

小学乘数不在main()的范围内,它只存在于start()中。

You can pass it into main.

你可以把它传递给main。

change the call to main to be main(gradschoolmultiplier)

把呼叫中心改为主要的(小学乘法器)

change def main() to def main(gradschoolmultiplier):

将def main()更改为def main(小学乘数):

Information on python scoping

python的信息范围