【发布时间】:2021-01-10 04:14:43
【问题描述】:
import random
x = random.randint(0,20)
y = int(input("guess the number from 0 to 20: \n"))
while True:
if y == x:
print("well done")
print(x)
stoper=input()
break
elif y < x:
print("too low")
stoper=input()
break
elif y > x:
print("too high")
stoper=input()
break
else:
stoper=input()
break
就是代码。这个脚本从 0 到 20 取一个随机数,让用户猜测它。如果猜测大于/小于数字,程序会打印信息和数字。如何在不改变号码x的情况下再次猜出号码?
如果有人想知道stoper 输入变量是不是这样,scripr 在运行代码后不会立即关闭。至于再次运行而不关闭它,问题是指脚本再次使用新的x和新的猜测而不关闭脚本。
【问题讨论】:
-
将带有
input的行放入while循环和break仅当x==y时。