Kenny101
Kenny101

Reputation: 11

Returning a user provided value from a function

My function should return a user's provided input. But, every time I try to run this function and use the variable outside of it I get an error saying that the variable is not defined. What am I missing?

#function to define user input number
def num_selection():
    x = input("Pick a number between 1 and 10:  ")
    return x

#Run number selection
num_selection()

print(x)

Upvotes: 1

Views: 43

Answers (2)

Elliott Frisch
Elliott Frisch

Reputation: 201447

Assign the returned value. This

num_selection()

should be

x = num_selection()

so that you can then

print(x)

or just print the result directly like

print(num_selection())

Upvotes: 1

andreihondrari
andreihondrari

Reputation: 5833

you are using x outside of the scope of the function num_selection.

Do something like:

print(num_selection())

Upvotes: 1

Related Questions