I'm new to python and I'm writing a practice script that generates a random number between 1 and 20, asks you to guess the number until you get it right and then prints the amount of guesses once you get it right. The code all works until I get the last number then I get this error:
Traceback (most recent call last):
File "C:\Users\James Brown\Desktop\guessnumber.py", line 20, in <module>
guess()
File "C:\Users\James Brown\Desktop\guessnumber.py", line 17, in guess
guess()
File "C:\Users\James Brown\Desktop\guessnumber.py", line 17, in guess
guess()
File "C:\Users\James Brown\Desktop\guessnumber.py", line 17, in guess
guess()
File "C:\Users\James Brown\Desktop\guessnumber.py", line 17, in guess
guess()
File "C:\Users\James Brown\Desktop\guessnumber.py", line 18, in guess
print("Good job! You guessed my number in " + tries)
TypeError: Can't convert 'int' object to str implicitly
Heres the code
import random
number = random.randint(1,20)
tries = 0
print ("I am thinking of a number between 1 and 20")
def guess():
print (number)
global tries
myGuess = int(input("Take a guess. "))
while (myGuess != number):
if (myGuess > number):
print ("Your guess is too high.")
else:
print ("Your guess is too low.")
tries = tries+1
guess()
print("Good job! You guessed my number in " + tries)
guess()
Can someone explain whats going on and how to prevent this? Thanks!