Duncan Duncan -4 years ago 157
Python Question

Name Error in Python 3.6

I am new to coding and am working on a simple math project.
I have the following code:

#!/usr/bin/env python3
import sys; print(sys.version)
import random

##Creates values to use to make random equation.

x = random.randint(1,11)
y = random.randint(1,11)
if x > y:
total = random.randint(x, 20)
else:
total = random.randint(y, 20)

##Creates actual values for A,B,C, and D in equation A+B=C+D

a = x
b = total - a
c = y
d = total - y

##Prints option choices and asks for user input

def start_game_message():
print ("Please fill in the blanks to make the following equation true:")
print ("__+__=__+__")
print ("The option choices are:" + str(a) + ", " + str(b) + ", " + str(c) + ", " + str(d))
def ask_for_input():
blank1 = input("What is the value of the first blank?")
blank2 = input("What is the value of the second blank?")
blank3 = input("What is the value of the third blank?")
blank4 = input("What is the value of the fourth blank?")
start_game_message()
##Check if user input is correct
def check_answer():
ask_for_input()
print (blank1)
if int(blank1)+ int(blank2) == int(blank3) + int(blank4):
print ("That is correct!")
else:
print ("That is incorrect. Please try again.")
ask_for_input()
check_answer()


When running this I get the following error:

Traceback (most recent call last):
File "C:/Users/duncs/PycharmProjects/moms_app/main", line 42, in <module>
check_answer()
File "C:/Users/duncs/PycharmProjects/moms_app/main", line 36, in check_answer
print (blank1)
NameError: name 'blank1' is not defined


Am I doing something wrong? I input values for each blank that should be stored. If I put print(blank1) within the ask_for_inputs function, it prints just fine. But when I call the function later on inside the check_answers function it results in the error. Can i not call a function in another function?
Please help! Thank you.

Answer Source

I think the best way to approach this would be to fix your ask_for_input():

def ask_for_input(which):
    return input("What is the value of the %s blank?" % which)
start_game_message()
##Check if user input is correct
def check_answer():
    inputs = []
    for blank in ['first', 'second', 'third', 'fourth']:
        inputs.append(ask_for_input(blank))

    if int(inputs[0])+ int(inputs[1]) == int(inputs[2]) + int(inputs[3]):
        print ("That is correct!")
    else:
        print ("That is incorrect. Please try again.")
        ask_for_input()
check_answer()

This avoids your scope issue by passing the result back via return. It also reduces code repetition and takes advantage of a list to store the 4 inputs.

As to why your code doesn't work, it's because if you examined the stack you'd see:

global
    check_answer
        ask_for_input
        -blank1
        -blank2
        -blank3
        -blank4

When ask_for_input returns, it's stack frame is lost:

global
    check_answer

So you have to figure out how to get those results out, either by assigning to variables which are in a broader scope (the suggestions for global) or via return.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download