1

I am getting an error when I try to run this simple python script:

def ask_x():
    x = int(input('What is X?'))

def ask_y():
    y = int(input('What is Y?'))

def result():
    print(z)

def count():
    if (x>10):
        z = x + y
    else:
        z = 0
        print('nono')


#start of program
ask_x()
ask_y()
count()
result()

I am using Python 3. I tried searching the forum and found Stackoverflow - input() error - NameError: name '…' is not defined but it doesn't work for me.

Jonathan
  • 311
  • 4
  • 14

4 Answers4

0

One way to get around scoping is to return the variable you need from your function and pass it in where needed. I prefer this to using global variables:

def ask_x():
    return int(input('What is X?'))

def ask_y():
    return int(input('What is Y?'))

def result(z):
    print(z)

def count(x,y):
    if (x>10):
        z = x + y
    else:
        z = 0
        print('nono')
    return z

#start of program
x = ask_x()
y = ask_y()
z = count(x,y)
result(z)

It would be better to use one of the ways presented in How to ask user for valid input to get to your input:

def askInt(text):
    """Asks for a valid int input until succeeds."""
    while True:
        try:
            num = int(input(text))
        except ValueError:
            print("Invalid. Try again.") 
            continue
        else:
            return num

x = askInt("What is X?") 
y = askInt("What is Y?")

This way you pass in the changing value (the text) and both profit from the variable parsing and validation.

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
0

This is because your variables are in a local scope. You can't access x outside of the ask_x() function.

I would suggest you read up on functions to get a better grasp of this.

def ask_x():
    return int(input('What is X?'))

def ask_y():
    return int(input('What is Y?'))

def result(z):
    print(z)

def count(x, y):
    if (x>10):
        return  x + y
    else:
        print('nono')
        return 0


#start of program
x = ask_x()
y = ask_y()
z = count(x, y)
result(z)

This will grab the values in each function, however, instead of storing them in the local scope, it'll be returned to the main function and stored in the corresponding variable.

You can then send x and y as parameters to count(), take care of your logic, and return the the value to be stored as z.

I hope this makes sense!

ltd9938
  • 1,444
  • 1
  • 15
  • 29
0

If you dont want to return then just initialize variable with some default values

x=0
y=0
z=0
def ask_x():
    global x
    x = int(input('What is X?'))

def ask_y():
    global y
    y = int(input('What is Y?'))

def result():
    global z
    print(z)

def count():
    global x,y,z
    if (x>10):
        z = x + y
    else:
        z = 0
        print('nono')


#start of program
ask_x()
ask_y()
count()
result()
mad_
  • 8,121
  • 2
  • 25
  • 40
0

Python follows function scoping unlike some other languages like c which follows block scoping. This implies variables defined inside a function cannot be accessed outside. unless they are defined global.

Solution to your problem: You can either return them in your functions and store them in variables in global scope or put all the input statements inside a single function.

himank
  • 439
  • 3
  • 5