0

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!

  • 2
    You can't add a number to a string. So either convert the number to a string with `str(tries)`, or change that `+` to a comma and let `print` do the conversion for you. Note that there are various other problems with your code. – PM 2Ring May 27 '16 at 23:51

2 Answers2

1

You need to convert your int tries to a string:

str(tries)

New 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 " + str(tries))

guess()

EDIT As mentioned in the comments, this also works. The print function will convert to you implicit.

print("Good job! You guessed my number in " , tries)
trinaldi
  • 2,872
  • 2
  • 32
  • 37
1

You need to convert the int to a string, you can convert using the str() function.

The reason it fails is because Python doesn't know how to add a number to text, so if you tell Python to give you a text version of the number, then you can join it to the text.

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 " + str(tries))

guess()
Morgoth
  • 4,935
  • 8
  • 40
  • 66