0

当输入 NO 或 N 时文件一直在运行,为什么?

while True:
        game_choice = input('Do you want to play? ').lower()

    if game_choice == 'yes' or 'y':
        break
    elif game_choice == 'no' or 'n':
        sys.exit()
    else:
        print('Please answer only Yes/y or No/n')
        continue
4

1 回答 1

1

您的 or 陈述不正确or y总是正确的,试试这个:

import sys

while True:
    game_choice = str(input('Do you want to play? ')).lower()
    if game_choice == 'yes' or game_choice == 'y':
        print("yes")
        break
    elif game_choice == 'no' or game_choice == 'n':
        sys.exit()
    else:
        print('Please answer only Yes/y or No/n')
        continue

或更好的版本:

import sys

while True:
    game_choice = str(input('Do you want to play? ')).lower()
    if game_choice in ['yes', 'y']:
        print("yes")
        break
    elif game_choice in ['no', 'n']:
        sys.exit()
    else:
        print('Please answer only Yes/y or No/n')
        continue
于 2019-07-03T07:02:42.160 回答