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.

1 Like

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

1 Like

This was a bit hard at first as I don’t fully understand how to write loop code yet. But finally I figure it out after some reviewing from previous lessons and some trial and error.

So I did two versions as well. They are both correct, right? Here is the first one:

print("Guess the Number Game")
print()
attempt = 0
while True:
  print("what number is in your mind? ")
  Number = int(input("Pick a number between 0 and 1000000"))
  attempt += 1
  if Number >= 1000000:
    print("too high!")
    continue
  elif Number == 0:
    print("too low!")
    continue
  elif Number == 500000:
    print("You got it! 🥳")
    print(attempt)
    print("it took you", attempt, "attempts!")
    break
  elif Number < 0:
    print("not playing this anymore!")
    exit()
  else:
    print("keep trying!")

Second one which I admitted I did peek at the solutions a bit to see if I’m doing it correctly or not.

print("Guess the Number Game")
print()
actual_number= 500000
attempt = 0
while True:
  print("what number is in your mind? ")
  guess_number = int(input("Pick a number between 0 and 1000000"))
  attempt += 1
  if guess_number > actual_number:
    print("too high!")
    continue
  elif guess_number == 0:
    print("too low!")
    continue
  elif guess_number == actual_number:
    print("You got it! 🥳")
    print(attempt)
    print("it took you", attempt, "attempts!")
    break
  elif guess_number < 0:
    print("not playing this anymore!")
    exit()
  else:
    print("keep trying!")

If the user says anything between 500k and 1m, it’ll say “keep trying!”, which won’t help the user understand that they need to go lower.

Also, I recommend you format your numbers like 1_000_000 and 500_000, it makes them much more readable.

2 Likes

If the user says anything between 500k and 1m, it’ll say “keep trying!”, which won’t help the user understand if they need to go higher.

What would you suggested I do in this case then to make the user understand?

Also, I recommend you format your numbers like 1_000_000 and 500_000, it makes them much more readable.

I see. Thank you for your suggestions!

Simply change your first if to

if Number > 500_000:
2 Likes

I need some help on this, I’m pretty sure the code is correct everywhere else however it says invalid syntax on line 8. Help would be appreciated

Ah, you forgot a closing ) on the line prior, that’s all it is.

3 Likes

That and the if statement is indented too much.

1 Like