Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
unexpected output
#1
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!")
Reply
#2
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'
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Unexpected output Starter 2 439 Nov-22-2023, 12:08 AM
Last Post: Starter
  Unexpected Output - Python Dataframes: Filtering based on Overlapping Dates Xensor 5 657 Nov-15-2023, 06:54 PM
Last Post: deanhystad
  Unexpected output while using random.randint with def terickson2367 1 469 Oct-24-2023, 05:56 AM
Last Post: buran
  Unexpected output from df.loc when indexing by label idratherbecoding 6 1,127 Apr-19-2023, 12:11 AM
Last Post: deanhystad
  unexpected output asyrafcc99 0 1,479 Oct-24-2020, 02:40 PM
Last Post: asyrafcc99
  Unexpected output: symbols for derivative not being displayed saucerdesigner 0 2,012 Jun-22-2020, 10:06 PM
Last Post: saucerdesigner
  Unexpected output palladium 4 2,701 Jan-11-2020, 03:26 PM
Last Post: palladium
  Unexpected output: if statement CabbageMan 1 1,732 Sep-04-2019, 04:12 PM
Last Post: ThomasL
  Unexpected Output using classes and inheritance langley 2 1,914 Jul-04-2019, 09:33 AM
Last Post: langley
  float multiplication - unexpected output inesk 3 3,258 Dec-11-2018, 10:59 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020