首页 > 解决方案 > 循环顺序问题

问题描述

我写了这个简单的代码:

alpha=float(input('For the MC Test for the mean define alpha (0.10 and 0.05 only available at the moment): '))
if alpha!=0.05 or alpha!=0.10:
    while True:
        print('Please insert a value equal to 0.05 or 0.10')
        alpha=float(input('For the MC Test for the mean define alpha (0.10 and 0.05 only available at the moment): ')
else:
    print('MC test will control the FWER at exactly {}% (balanced test)'.format(alpha))

然而,它正在创建一个循环,即使我输入 0.05,它也会再次要求插入 alpha。我会很感激你的意见。谢谢。

标签: pythonloopsinput

解决方案


只需重新排列循环:

while True:
    if alpha not in [0.05,0.1]:
        print('Please insert a value equal to 0.05 or 0.10')
        alpha=float(input('For the MC Test for the mean define alpha (0.10 and 0.05 only available at the moment): '))
    else:
        print('MC test will control the FWER at exactly {}% (balanced test)'.format(alpha))
        break

推荐阅读