Skip to content Skip to sidebar Skip to footer

Condition Checking In Python With User Input

I tried taking an input from keyboard. the checking that input with an if else statement. But everytime the else part is working. The if statement does not happen to be true. I can

Solution 1:

Your input variable is a string. You need to cast it to an integer to correctly compare it to 6.

if int(abc) == 6:

Solution 2:

raw_input returns a string. abc is a string, and a string will never be equal with an integer. Try casting abc or the return value of raw_input(). Or, you can make 6 a string.

Casting the return value ofraw_input():

abc = int( raw_input('Enter a 2 digit number') )

Castingabc:

abc = int(abc)

or

if int(abc) == 6:

Changing6to string :

ifabc== '6':

Solution 3:

>>>abc= raw_input("Enter a  2 digit number")
Enter a  2 digit number6
>>>ifint(abc) == 6:
    print "Its party time!!!"


Its party time!!!
>>>

Post a Comment for "Condition Checking In Python With User Input"