-1

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!

4
  • check your type. Is input a string? Commented Dec 5, 2017 at 0:13
  • Is this your actual code? If so, you need to do var = input("press ENTER to choose an app") to store the input value in var. Commented Dec 5, 2017 at 0:15
  • First of all, your indentation is bad in the question. Assuming it's fine in your own code, var has been set equal to the input function, and pressing ENTER twice doesn't really make sense Commented Dec 5, 2017 at 0:16
  • thanks for all the info/help! Commented Dec 5, 2017 at 0:44

4 Answers 4

2

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!

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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.

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.