Learning Python: If condition executing all the time

后端 未结 5 1511
情深已故
情深已故 2021-01-21 05:33

I am learning python and i can\'t figure out why the following program is printing your number is greater than what i thought even when the guessed number is sm

5条回答
  •  太阳男子
    2021-01-21 06:30

    Here datatype of guess is string where as datatype of number is integer,

    guess = raw_input ("Guess:")
    

    so both are not same, hence if statement is executed every time

    if guess > number:
    

    as string datatype is considered greater than integer datatype in python.

    Therefor you need to change the datatype of guess to int.

    Following code will help you do this

    guess = int(raw_input ("Guess:"))
    

提交回复
热议问题