#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.
答案 0 :(得分:3)
您的代码中存在打字错误
guesesTaken = str(guessesTaken)
应该是
guessesTaken = str(guessesTaken)
答案 1 :(得分:2)
假设缩进在问题中搞砸了,并且在你的作业文件中是正确的(Lab4.py) 尝试:
print('Good job, ' + myName + '! You guessed my number ' + str(guessesTaken) + ' guesses!')
答案 2 :(得分:0)
使用str()L
将int转换为字符串print('Good job, ' + myName + '! You guessed my number ' + str(guessesTaken) + ' guesses!')