0
#Guess the number game by Johnathan Millsap
import random

guessesTaken = 0

print('Hello! What is your name?')
myName = input()

number = random.randint(1, 100)
print('Well, ' + myName + ', I am thinking of a number between 1 and 100.')
print('Try to guess it!.')

while guessesTaken <= 3:
    print('Take a guess.')
    guesesTaken = guessesTaken + 1
    guess = input()
    guess = int(guess)

if guess < number:
    print('Your guess is too low.')     
if guess > number:
    print('Your guess is too high.')
if guess == number:


if guess == number:
    guesesTaken = str(guessesTaken)
    print('Good job, ' + myName + '! You guessed my number ' + guessesTaken + ' guesses!')

if guess != number:
    number = str(number)
    print('Nope. The number I was thinking of was ' + number)

我遇到的主要问题是它一直告诉我我的 int 不会隐式转换为字符串......我也试图在 1-100 和 1-1000 和 1-10000 等三个不同的困难中做到这一点。对此的任何帮助将不胜感激。

Traceback (most recent call last):
  File "Lab4.py", line 28, in <module>
    print('Good job, ' + myName + '! You guessed my number ' + guessesTaken + ' guesses!')
TypeError: Can't convert 'int' object to str implicitly
Terminated with exit code 1.
4

3 回答 3

3

您的代码中存在印刷错误

guesesTaken = str(guessesTaken)

应该

guessesTaken = str(guessesTaken)
于 2012-12-04T18:23:51.250 回答
2

假设问题中的缩进搞砸了,并且在您的作业文件(Lab4.py)中是正确的尝试:

print('Good job, ' + myName + '! You guessed my number ' + str(guessesTaken) + ' guesses!')
于 2012-12-04T18:23:34.557 回答
0

使用 str()L 将 int 转换为字符串

print('Good job, ' + myName + '! You guessed my number ' + str(guessesTaken) + ' guesses!')
于 2012-12-04T18:26:11.077 回答