[Tutor] comparison on Types

Ian D duxbuz at hotmail.com
Wed Apr 29 11:44:28 CEST 2015


I was looking at the example code below. I am using python 2.7.

I am wondering why when I substitute the while n! = "guess" to while n!= guess (<-- no quotes) I get a problem?

The Type string is used for the first conditional comparison in the outer While loop, but afterwards the Type is an int.

I would have expected the guess variable to be used as Type int as it seems to be cast in the raw_input statement and would be comparable to another int that's stored in variable n. Thanks


import random
n = random.randint(1, 99)
guess = int(raw_input("Enter an integer from 1 to 99: "))
while n != "guess":
    print
    if guess < n:
        print "guess is low"
        guess = int(raw_input("Enter an integer from 1 to 99: "))
    elif guess> n:
        print "guess is high"
        guess = int(raw_input("Enter an integer from 1 to 99: "))
    else:
        print "you guessed it!"
        break
    print  		 	   		  


More information about the Tutor mailing list