Day 018 - Project 18 : Guess the Number

If you have any questions, comments or issues with this project please post them here!

1 Like

Hi Ian! I’m currently scratching my head at this error that python is throwing at me.
the error is " type error: “>=” not supported between instances of ‘int’ and ‘str’. here’s part of my code:

print (“guess the number challenge”)
print()
count1 = 0
while True:
print (“pick a number between 0 and 100”)
number = int(input("what is your guess? > "))
print()
if number >= “90” and number <= “100”:
print (“too high”)
count1 += 1
continue
print()
elif number >= “80” and number <= “89”:
print (“still too high”)
count1 += 1
continue
print ()

The error is as the message states, Python cannot tell if a string is greater than or less than an integer.
Hence, this will cause an error:

if 1 > "1":

You will need to do:

if 1 > 1:

Your code should probably look like this:

print("guess the number challenge")
print()
count1 = 0
while True:
	print ("pick a number between 0 and 100")
	number = int(input("what is your guess? > "))
	print()
	if number >= 90 and number <= 100:
		print ("too high")
		count1 += 1
		continue
	elif number >= 80 and number <= 89:
		print ("still too high")
		count1 += 1
		continue

By adding quotation marks (") to the numbers you are comparing, you made them strings.

3 Likes

omg thank you so much!

1 Like

@MattDESTROYER it’s past midnight and i think my brain is partly fried but thank you again! the program works as expected now

1 Like

I would suggest getting some sleep then, it’s 11:30am on a Sunday for me lol.

1 Like

Can you “Guess the Number” I have in mind :thinking:?

https://replit.com/@JackAdem/Day-018-Project-18-Guess-the-Number?v=1

Day 18 of #Replit100DaysOfCode #100DaysOfCode.

1 Like

Hiya, i just need help with this code:
I do not understand why the else statement is not working. The idea is , if the user inputs anything that isn’t a whole number, it should print “That is not a number” but i keep getting an error. My code is below:

Hi @dbpwygwhzy thanks for your post and welcome to the community!

The reason you see the error is because in line 7 the input is being converted into an integer, so it’s not even getting to the if statement.

You can stop the error from occurring by replacing line 7 with a try... except block. For example:

guess = -1
while guess < 0:
  try:
    guess = int(input("Input the number "))
    while guess < 1 or guess > 1000000:
         print("Enter a number between 1 and a million only.")
         guess = int(input("Input the number "))
  except:
    print("That is not a number")
    guess = -1

You have also made separate if statements, I believe you meant to make an if chain, meaning those second two if statements should become elif statements. Otherwise the else statement is only attached to the final if statement as opposed to all of them:

# this forms one long chain of if conditions ending in an else condition
if condition1:
  # ...
elif condition2:
  # ...
elif condition3:
  # ...
else:
  # ...
# this is the equivalent of what you have
if condition:
  # ...
# not connected
if condition:
  # ...
# not connected
if condition:
  # ...
else:
  # ...
1 Like

thanks everyone, I’ve only just found this help thread!!! I’ve been spending hours on what should be 15 minutes of code!!! Here’s my code that works thanks to you all (I don’t know how to post the colours here tho, sorry about that)

print("What's my number?")


guess_no = 0

while True:
  guess=int(input(">"))
  
  if guess >= 101:
    print("Too high, try again.")
    guess_no += 1
    continue
    
  elif guess <= 99:
    print("Too low, try again.")
    guess_no += 1
    continue
    
  else:
    if guess == 100:
      print("Yippee, you got it in" , guess_no)

    break

thanks again everyone

I’ve done it for you, though.

wicked - thanks heaps, I’ll do that next time :slight_smile:

1 Like