gpt4 book ai didi

python - 如何让我的 while 循环在 Python 中工作?

转载 作者:太空宇宙 更新时间:2023-11-03 19:50:34 24 4
gpt4 key购买 nike

这是我的代码:

import random
Random = random.randint(1, 10)
print("Number:" + str(Random))
Number = int(input("Guess the number I am thinking of from 1-10"))
while int(Random) != Number:
if(Random > Number):
Number = input("Too low. Guess again!")
elif(Number > Random):
Number = input("Too high. Guess again!")
print("You guessed it!")

当猜到正确的数字时,就会发生这种情况,这是应该发生的。

Number:8
Guess the number I am thinking of from 1-10 8
You guessed it!

但是,当数字没有被正确猜出时,它只会循环通过 elif 语句。

Number:10
Guess the number I am thinking of from 1-10 6
Too low. Guess again! 7
Too high. Guess again! 6
Too high. Guess again! 5
Too high. Guess again! 4
Too high. Guess again! 3
Too high. Guess again! 2
Too high. Guess again! 1
Too high. Guess again! 10
Too high. Guess again! 9
Too high. Guess again! 8

最佳答案

您是否尝试过将 int 转换为 while 循环中两个输入行中的输入?当它像这样时,它似乎对我有用:

if(Random > Number):
Number = int(input("Too low. Guess again!"))
elif(Number > Random):
Number = int(input("Too high. Guess again!"))

关于python - 如何让我的 while 循环在 Python 中工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59870550/

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