Python Forum
unexpected output - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: unexpected output (/thread-3029.html)



unexpected output - IICatzII - Apr-25-2017

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!")



RE: Help :D - buran - Apr-25-2017

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'