循环顺序问题



我写了这个简单的代码:

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。我非常感谢你的评论。谢谢

您可以重新安排if语句以首先检查正确的输入,然后在else语句和代码中再次读取数据,并在获得正确的输入后添加break语句。

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:
print('MC test will control the FWER at exactly {}% (balanced test)'.format(alpha))
else:
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): ')
if alpha==0.05 or alpha==0.10:
print('MC test will control the FWER at exactly {}% (balanced test)'.format(alpha))
break

只需重新排列循环:

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

最新更新