1

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!

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
user3125424
  • 21
  • 1
  • 1
  • 2
  • Try `if gradschool.lower() in ("y", "yes")` and put in a line break after each `:`, then combine your `print` and assignment of `g1` into the same block. – jonrsharpe Dec 21 '13 at 15:18
  • 2
    http://www.python.org/dev/peps/pep-0008/ - you REALLY need to read and understand this document. Also, if you repeat the exact same code multiple times YOU ARE DOING SOMETHING WRONG (see @jonrsharpe's comment). Having a `main` method but using `start()` as the "main" method is a bad idea, too... – ThiefMaster Dec 21 '13 at 15:37

2 Answers2

2

Indeed as @Dan says, the scoping problem.

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.

Oh, we share the same thoughts @jonrsharpe

Quick improvement as suggested by @Nils

Ray
  • 2,472
  • 18
  • 22
1

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

You can pass it into main.

change the call to main to be main(gradschoolmultiplier)

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

Information on python scoping

Community
  • 1
  • 1
Daniel Rucci
  • 2,822
  • 2
  • 32
  • 42
  • You are a genius! Thank you. Would you be so kind as to explain what the def main() is doing vs. def main(gradschoolmultiplier)? – user3125424 Dec 21 '13 at 15:23
  • `def main(a)` means that when main is called, it expects 1 variable to be passed into it, and in the code of `main()` that variable will be referred to as `a`. `main()` is not expecting anything to be passed to it. – Daniel Rucci Dec 21 '13 at 15:38