Python Forum

Full Version: unexpected output
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
my question here
I'm very new to Python and I created a simple number guessing game and for some reason when i enter a number that is 100 or higher it will say Higher. Why?

VarNum = "0"
while(VarNum != "27"):
VarNum = input("Guess my number from 1-99: ")
print("Your answer is:", VarNum)
if (VarNum > "27"):
print("Lower")
elif (VarNum < "27"):
print("Higher")
elif (VarNum == "27"):
print("Congratulations! You guessed my number!")
you are working with str, not int, which is wrong. you should work with numbers, not strings. when comparing strings it compares first char of str1 with first char of str2, so if you enter '100' (that is str), it wil compare char '1' with first char of '27', i.e. '2'. '1' comes before '2' so it's says 'Higher'