为什么我的程序在这里出错?
import random
TheNumber = random.randrange(1,200,1)
NotGuessed = True
Tries = 0
GuessedNumber = int(input("Take a guess at the magic number!: "))
while NotGuessed == True:
if GuessedNumber < TheNumber:
print("Your guess is a bit too low.")
Tries = Tries + 1
GuessedNumber = int(input("Take another guess at the magic number!: "))
if GuessedNumber > TheNumber:
print("Your guess is a bit too high!")
Tries = Tries + 1
GuessedNumber = int(input("Take another guess at the magic number!: "))
if GuessedNumber == TheNumber:
print("You've guess the number, and it only took you " + string(Tries) + "!")
错误出现在最后一行。我能做什么?
编辑:
另外,为什么我不能在 Python 中使用 Tries++?不是有自增代码吗?
编辑2:错误是:
Traceback (most recent call last):
File "C:/Users/Sergio/Desktop/GuessingGame.py", line 21, in <module>
print("You've guess the number, and it only took you " + string(Tries) + "!")
NameError: name 'string' is not defined
最佳答案
在最后一行中,将 string
替换为 str
——至少应该解决 python 提示的错误。
关于python - 运行一个非常简单的 Python 程序时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1522708/