fdama
fdama

Reputation: 304

Type Conversion error in 'Guess my number' game

I am writing code in Python 3 for the 'Guess my Number' game. In this version however, the computer has to guess a number that user has kept secret. I am new to Python and programming in general and would appreciate a bit of help. I am getting an error that relates to type conversion. Here is the error:

    Traceback (most recent call last):
  File "C:\Users\Documents\python\GuessMyNumberComputer.py", line 16, in <module>
    response = input("Is the number" +guess +"? \n Press (y - yes, l - go lower, h - go higher)")
**TypeError: Can't convert 'int' object to str implicitly**

And here is my code:

 import random

print("\t\t\t Guess My Number")
input("Think of a number between 1 and 100 and I will try to guess it. \nPress enter when you have thought of your number and are ready to begin.")
a = 1
b = 100
tries = 0

while 1==1:
    guess = random.randint(a,b)
    response = input("Is the number" +guess +"? \n Press (y - yes, l - go lower, h - go higher)")
    tries += 1
    if response == y:
        break

    elif response == l:
        b = response-1

    elif response == h:
        a = response+1


print("Aha! I guessed it! And it only took",tries,"tries!")
input("Press enter to exit.")                 

Could someone help me with this error? Could you also point me to some links on the web so I can read up on this as my book seems not to cover this area.

Thanks.

Upvotes: 3

Views: 307

Answers (3)

khagler
khagler

Reputation: 4056

The other answers have already addressed your TypeError, so I'll add something for you to read:

Python 3.3 Tutorial

Upvotes: 0

Aesthete
Aesthete

Reputation: 18848

You should use format() when constructing strings from different types. It makes your intentions clearer.

response = input('Is the number {}? \n Press...'.format(guess))

Upvotes: 0

Linuxios
Linuxios

Reputation: 35788

Just pass the int to the str() constructor to convert it to a string. So here's the new line:

response = input("Is the number" + str(guess) +"? \n Press (y - yes, l - go lower, h - go higher)")

Upvotes: 3

Related Questions