TheDeveloperNextDoor
TheDeveloperNextDoor

Reputation: 195

There's an issue with Input in my Python code

so I'm a beginner in python and I was trying to get an input function to work. It looks to me like Python isn't taking the data I give it, like it's not reading user input correctly. here is my code:

var = input
    input("press ENTER to choose an app")
    if var==1:
        clock() 
    elif var==2:
        oshelp()
    elif var==3:
        ebooks()
    elif var==4:
        what_is_new()
    else:
        print("Application Not Found.")

right now, the IDLE just prints "Application Not Found" even when i type a valid number and I'm not sure why. can anyone help me with this? (please include examples). Thanks!

Upvotes: -1

Views: 277

Answers (4)

Stephen
Stephen

Reputation: 1099

The python input returns a string and you are comparing ints. If you would like to compare ints, then:

inputInt = int(input("please ENTER"))

or you could use eval

inputInt = eval(input("please ENTER"))

be careful with eval as it can cause problems, but it will handle just numbers and floats for you.

Upvotes: 0

DireDan
DireDan

Reputation: 101

Your issue occurs on the first line

var = input

You are setting var equal to the function input, not the returning value.

How you have it, if you were to write x = var("Enter: "), this would do the same as x = input("Enter: ").

You actually need to do var = input("Enter: "), but this will return a value, of type string, so when you compare this value to 1, even if the user enters 1, it will return false, as they are different data types.

You can either cast the input to an integer value, or compare the inputted value to strings.

var = input("Enter: ")
if var == "1":

or

var = int(input("Enter: "))
if var == 1

I would personally use the top one, as the program wouldn't crash if entered a non-int value.

Hope this helps!

Upvotes: 2

rassar
rassar

Reputation: 5660

input returns a string, but you're checking it against ints. One way to do this would be to check the input, as explained here. You could also just compare it to strings:

if var == '1':

Or convert the input to an int directly:

var = int(input(...))

Be careful with the last one, as it will fail if the user does not input a valid int.

Upvotes: 0

anon
anon

Reputation: 1258

The input will be a string and not ints. You can change your conditions from checking var == 1 to var == "1" etc. Or you can create an int from the input, using int(input()). However beware of the case where the input is not convertible to an int in that case an exception will be thrown.

Upvotes: 0

Related Questions