python - 运行一个非常简单的 Python 程序时出现问题

标签 python ide python-idle

为什么我的程序在这里出错?

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/

相关文章:

python - 处理特定的和一般的 Python 异常?

ide - 比例字体IDE

Python3 : How to define exceptions in method headers?

python - 如何在 Spyder 的绘图 Pane 中启用放大/缩小和缩放到百分比按钮?

python - Python IDLE shell 中的清除行

python-idle - 如何在 IDLE 中清除 Python Shell

python - 如何从 Python 中的 stdout 中删除行 - 在 SciTe、Idle、Eclipse 或其他带有控制台的编辑器中

python - 是否可以在不将 Pyramid 应用程序安装为鸡蛋的情况下运行它?

python - 删除特殊转义python

python - Li-Fi通信中的编码/解码问题