> Python Code Help?

Python Code Help?

Posted at: 2014-12-18 
There is no version 2.8 of Python. Maybe you mean 2.7.8? Anyway, the main difference is between Python 2.x and Python 3.x and we knew that it was 2.x as soon as you used print as a statement instead of a function call.

The raw_input() function returns a string, and you can't compare strings to numbers. You can convert the string to a number with int(), or your can compare to '1' instead of 1. The second approach is easiest, I think.

if (x == '1'):

... etc.

This has the advantage of not raising any exceptions when trying to convert user input to a number. If the user types garbage, it simply won't match any of your comparisons in if or elif statements. Adding an else at the end to handle garbage is easier than try/except to catch exceptions.

code academy

x = int (raw_input ())

Today, I was bored and decided to do a random Python file. I created a menu where, to choose an option, you type the corresponding number and press enter, but it doesn't work. Here's the bit of code in question,

print "1) Option 1"

print "2) Option 2"

print "3) Option 3"

print "4) Option 4"

print "5) Option 5"

x = raw_input ()

if (x == 1):

On the line where it says 'raw_input', I type, for example, 1 and it completely stops. How can I fix this?