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

标签 python loops while-loop

这是我的代码:

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/

相关文章:

python - 图中垂直子图的最大数量

python - 如何从词序中区分句子型和非句子型?

java - 需要转换成二维数组的String ArrayList

c - 循环无法正常工作

python - 禁用 argparse 中类似参数的连接和缩写

python - 使用 Python 2.7/Windows 运行多处理作业

c++ - 带链接列表的哈希表中的前 10 个频率

c++ - while循环创建无限循环

c - 我的 while 循环不工作

用于查找素数的 Python while 循环