小编Fea*_*ENT的帖子

Python while循环不按预期工作

当我在输入中键入"no"时,我希望它将"1"添加到"x",从而结束循环,但会发生的是忽略它并且不添加1 x.这是代码.

x = 1
password = ""

while x == 1:        
    # imagine there is some code here which works

    ans1 = input("\n\nTest a new password? ")
    ans1 = ans1.upper()

    print(ans1)

    if ans1 == ("Y" or "YES"):
        x = x
    elif ans1 == ("N" or "NO"):
        x = x + 10

    print(x)
Run Code Online (Sandbox Code Playgroud)

这是底部if/elif语句不起作用.它应该继续再次请求输入,直到用户说"不",但这不起作用.

python loops while-loop

2
推荐指数
1
解决办法
2113
查看次数

标签 统计

loops ×1

python ×1

while-loop ×1