2

So I was doing a tuition program assignment for homework the other day. This is my program. I hope it's self explanatory. (This is Python, by the way.)

def print_intro():
print ("Welcome to the tuition program. This program will calculate your tuition cost.")

def get_id_num():
    grad_status = input("Is the student an undergrad or grad (U/G)? ")
    id_num = int(input("Enter the student's ID Number: "))
    while (grad_status == "U") and (id_num < 0):
        print("Sorry, that is invalid. Please try again.")
        id_num = int(input("Enter the student's ID Number: "))  
    while (grad_status == "G") and (id_num >= 0):
        print("Sorry, that is invalid. Please try again.")
        id_num = int(input("Enter the student's ID Number: "))

def get_info():
    get_id_num()
    age = int(input("Enter the student's age (numbers only): "))
    residency = input("Is the student in-state or out-of-state (I/O)? ")
    num_credits = int(input("How many hours of credit will the student take? "))
    correct = input("Is all the above information correct (Y/N)? ")
    while correct == "N":
        print("Please enter the information correctly!")
        get_info()
    else:
        get_base_tuition()

def get_base_tuition():
    if (grad_status == "U") and (num_credits <= 6) and (residency == "I"):
        initial_tuition = 2000
    if (grad_status == "U") and (7 <= num_credits <= 11) and (residency == "I"):
        initial_tuition = 3000
    if (grad_status == "U") and (num_credits >= 12) and (residency == "I"):
        initial_tuition = 3800
    if (grad_status == "U") and (num_credits <= 6) and (residency == "O"):
        initial_tuition = 2000
    if (grad_status == "U") and (7 <= num_credits <= 11) and (residency == "O"):
        initial_tuition = 3000
    if (grad_status == "U") and (num_credits >= 12) and (residency == "O"):
        initial_tuition = 9000
    if (grad_status == "G") and (num_credits <= 6) and (residency == "I"):
        initial_tuition = 2000
    if (grad_status == "G") and (7 <= num_credits <= 11) and (residency == "I"):
        initial_tuition = 2500
    if (grad_status == "G") and (num_credits >= 12) and (residency == "I"):
        initial_tuition = 4400
    if (grad_status == "G") and (num_credits <= 6) and (residency == "O"):
        initial_tuition = 2000
    if (grad_status == "G") and (7 <= num_credits <= 11) and (residency == "O"):
        initial_tuition = 3700
    if (grad_status == "G") and (num_credits >= 12) and (residency == "O"):
        initial_tuition = 4400
    print(initial_tuition)

So after I run this, the get_info() function runs fine, up until it goes to the get_base_tuition() function. I get this error:

line 28, in get_base_tuition
    if (grad_status == "U") and (num_credits <= 6) and (residency == "I"):
NameError: name 'grad_status' is not defined

Should I have been adding parameters in my functions or something? I'm stuck and really don't know what to do. I know that if I include the variables "grad_status", "num_credits", and "residency" inside of get_base_tuition() OR define the variables inside the Python Shell before running get_base_tuition(), then of course it will work. But if I put it inside another get_base_tuition() like I did now, it won't work. Is there any way to make the get_base_tuition() function run without having to put the variables inside of get_base_tuition()? Because I already put those variables inside the functions get_id_num() and get_info(). Help is appreciated, thanks.

In case you want to know about the way the base tuition is supposed to be calculated, I attached that assignment here: Assignment Page 2

Oleks
  • 1,633
  • 1
  • 18
  • 22
Crock Koo
  • 33
  • 1
  • 1
  • 5

1 Answers1

3

You need to read about and understand the concept of scope in Python. Here's a good answer for getting the basics on scope

Specifically in your code the grad_status variable is defined in the get_id_num function. Once the program exits that function, all the variables defined in that scope are erased and won't exist in the scope of other functions. If you have a variable that is needed in multiple functions, you need to define that variable in the global scope or pass it into each function that needs it as a parameter.

Community
  • 1
  • 1
gph
  • 116
  • 3
  • Just wanted to clarify, when I say the variables are "erased" that doesn't necessarily mean they are immediately disposed of. It's an important distinction, especially for resources that need to be explicitly closed such as DB connections. – gph Mar 07 '17 at 21:56
  • 1
    That really helped! I defined my variables in the global scope and everything worked! Really appreciate the help! – Crock Koo Mar 13 '17 at 01:11