If...else Statement Issue With Raw_input On Python
Solution 1:
The return value of raw_input
is a string, but when you check the value of food, you are using an int. As it is, if food == 1
can never be True
, so the flow always defaults to the plural form.
You have two options:
ifint(food)== 1:
The above code will cast food
to an integer type, but will raise an exception if the user does not type a number.
iffood== '1':
The above code is checking for the string '1' rather than an integer (note the surrounding quotes).
Solution 2:
In Python 2.x raw_input returns a string. Looking at your code, you could also use input which returns an integer. I would think that would be the most explicit option using Python2.
Then you can treat food as an int throughout your code by using %d instead of %s. When entering a non int your program would throw an exception.
Post a Comment for "If...else Statement Issue With Raw_input On Python"