1

I have this line of code:

incoming = input("Type in 1 or 2")

if incoming == 1:
    print ("you entered 1")
elif incoming == 2:
    print ("you entered 2")

this worked perfectly fine when I used python 2... on my mac, but on windows with python 3, not so well.

Can anybody explain this to me?

5
  • 1
    input() returns a string..... Commented Jun 20, 2016 at 9:51
  • in if-else structure replace incoming with int(incoming) Commented Jun 20, 2016 at 9:54
  • 1
    That's because in python 2 input(prompt) was roughly equivalent to eval(raw_input(prompt)). Commented Jun 20, 2016 at 9:54
  • any idea why it worked on mac with python 2.7? Commented Jun 20, 2016 at 9:54
  • With that in mind, read "Is using eval in python a bad practice?" Commented Jun 20, 2016 at 9:58

1 Answer 1

3

Python 3.x doesn't evaluate and convert data types the way Python 2.x did. So, you are going to have to explicitly convert your user's input to an integer like this:

incoming = int(input("Type 1 or 2: "))
Sign up to request clarification or add additional context in comments.

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.