# 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 ?

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

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