我如何"循环"这个其他声明?

Pol*_*heo 0 python python-3.x

我对Python很陌生,我在创建这个'测验'时遇到了麻烦.

answer_1_choices = {
    'A' : 'A: A list is a datatype that stores a sequence of items with individual indexes that are mutable.',
    'B' : 'B: A list is a datatype that stores a sequence of items with individual indexes that are immutable.',
    'C' : 'C: A list is a datatype that stores a sequence of items assigned to individual keys that are immutable.'
    }
Number_One = '1. What is a list?'

def answer():
    x = input()
    if x == 'A' or x == 'a':
        print('Correct!')
    else:
        print('That is an incorrect response.  Please try again.' + '\n' + '\n' + Number_One + '\n')
        print(answer_1_choices['A'] + '\n' + '\n' + answer_1_choices['B'] + '\n' + '\n' +  answer_1_choices['C'])
        print('\n' + 'Is the answer A, B, or C?  Type the letter of your choice.')

def question_one():
    print(Number_One + '\n')
    print(answer_1_choices['A'] + '\n' + '\n' + answer_1_choices['B'] + '\n' + '\n' +  answer_1_choices['C'])
    print('\n' + 'Is the answer A, B, or C?  Type the letter of your choice.')


question_one()
answer()
Run Code Online (Sandbox Code Playgroud)

每当输入不是'A'或'a'时,我希望else语句无限运行.我知道我必须使用某种循环或其他东西,但我似乎无法弄明白.有人能帮我吗?

小智 7

你在想什么是一个while循环.x = 'A' or x = 'a'您可以尝试测试是否x不是'A'或不是检查'a'.

试试这个:

while (x != 'A' and x != 'a'):
    print('That is an incorrect response.  Please try again.' + '\n' + '\n' + Number_One + '\n')
    print(answer_1_choices['A'] + '\n' + '\n' + answer_1_choices['B'] + '\n' + '\n' +  answer_1_choices['C'])
    x = input('\n' + 'Is the answer A, B, or C?  Type the letter of your choice.')
print('Correct!')
Run Code Online (Sandbox Code Playgroud)

这样,它只会打印"正确!" 一旦破裂条件得到满足.