一旦其中一个变量达到所需数量,我希望它停止.为什么这段代码要等到两个变量等于20或更大才结束?
z = 20
x = 1
y = 0
while x < z or y < z:
inp = int(input('enter a number'))
if x > y:
y += inp
elif y > x:
x += inp
print(x, y)
Run Code Online (Sandbox Code Playgroud)
或使用类似这些例子的东西只是不断添加,永不停止:
while x != z or y != z:
while x or y < z:
while x or y != z:
Run Code Online (Sandbox Code Playgroud)
如果循环必须在至少有一个变量时停止>= z,则必须使用and连接条件:
while x < z and y < z:
Run Code Online (Sandbox Code Playgroud)
在您的代码中,通过使用or您声明只要其中一个变量< z,循环必须继续 - 这不是您想要的.
| 归档时间: |
|
| 查看次数: |
4907 次 |
| 最近记录: |