Reputation: 1
import random
rnd=0
guessesTaken = 0
print('Hello! What is your name?')
myName = input()
for i in range (10):
print('---round' +str(rnd+1) +'---')
number = random.randint(1, 20)
print('Well, ' + myName + ', I am thinking of a number between 1 and 20.')
while guessesTaken <= 5:
***~the error ^^^~***
print('Take a guess.')
guess = input()
guess = int(guess)
guessesTaken = guessesTaken + 1
if guess < number:
print('Your guess is too low.')
if guess > number:
print('Your guess is too high.')
if guess == number:
break
if guess == number:
guessesTaken = str(guessesTaken)
print('Good job, ' + myName + '! You guessed my number in ' + guessesTaken + ' guesses!')
if guess != number:
number = str(number)
print('Nope. The number I was thinking of was ' + number)
rnd=rnd+1
I am trying to put a round system into this guessing game but at round two after it says "Well, aidan, I am thinking of a number between 1 and 20." there is an error saying
TypeError: '<=' not supported between instances of 'str' and 'int'
from line 15.
Upvotes: 0
Views: 39
Reputation: 3306
When you are saying guessesTaken = str(guessesTaken)
, the original guessesTaken
variable, which was an integer, becomes a string. Then, when you're verifying a second time your while
loop condition guessTaken <= 5
, you are comparing a string with an integer... which is not something that is supported, as your error stated '<=' not supported between instances of 'str' and 'int'
.
The solution would be, as one stated in the comment, to have a better understanding of string formatting, and printing usage. You don't need to transform entirely your variable (meaning transforming your integer into a string).
You could just do instead of...
guessesTaken = str(guessesTaken)
print('Good job, ' + myName + '! You guessed my number in ' + guessesTaken + ' guesses!')
This :
print('Good job, {} ! You guessed my number in {} guesses !'.format(myName, guessesTaken))
Doing this, you are simply printing your variable as is, instead of casting it into another type. If you would still do it your way (which is bad, by the way), you could do...
guessesTaken = str(guessesTaken)
print('Good job, ' + myName + '! You guessed my number in ' + guessesTaken + ' guesses!')
guessesTaken = int(guessesTaken)
Upvotes: 2