Проблемы с порядком зацикливания - PullRequest
1 голос
/ 19 апреля 2020

Я написал этот простой код:

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))

Однако он создает al oop, в котором, даже когда я набираю 0.05, он снова просит вставить альфа. Буду признателен за ваши комментарии. Спасибо.

Ответы [ 3 ]

1 голос
/ 19 апреля 2020

Вы можете переупорядочить оператор 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
1 голос
/ 19 апреля 2020

Просто переставьте петли:

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
0 голосов
/ 19 апреля 2020

Проблема заключается в том, что оператор if logi c. Если ввод равен 0,05, он все равно будет ложным, поскольку он не будет равен 0,1

if alpha != 0.05 or alpha != 0.10:
...