gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 22:07:52 26 4
gpt4 key购买 nike

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

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com