2012-12-10 3 views
3

'내 번호 추측'게임에 대한 Python 3 코드를 작성하고 있습니다. 그러나이 버전에서 컴퓨터는 사용자가 비밀로 유지 한 숫자를 추측해야합니다. 저는 파이썬과 프로그래밍에 익숙하지 않으며 약간의 도움에 감사 할 것입니다. 유형 변환과 관련된 오류가 발생합니다. 여기에 오류가 :'내 번호 추측'게임에서 유형 변환 오류가 발생했습니다.

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** 

는 그리고 여기 내 코드입니다 :

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.")     

누군가가이 오류와 함께 나를 도울 수는? 내 책이이 영역을 다루지 않는 것 같아서이 부분을 읽을 수 있도록 웹의 일부 링크를 가르쳐 주시겠습니까?

감사합니다.

+0

사용'동안 TRUE '대신'1 == 1 '동안. – ApproachingDarknessFish

답변

3

str() 생성자에 정수를 전달하여 문자열로 변환하면됩니다. 그래서 여기에 새로운 라인이다 : 다른 유형의 문자열을 구성 할 때

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

당신은 format()를 사용해야합니다. 그것은 당신의 의도를 더 분명하게 만듭니다.

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

는 다른 답변은 이미 TypeError를 해결 한, 그래서 당신은 읽을 뭔가를 추가 할 것입니다 :

Python 3.3 Tutorial

관련 문제