如何让 Python 只允许特定答案

How do I get Python to only allow a specific answer

你好我需要帮助我一直在做的这个程序,这是一个程序,它会问用户琐事问题,然后计算用户的智商,我设法让程序问用户问题,然后添加一个1 到名为 'IQ' 的列表,当用户正确回答问题时,我让计算机计算列表中有多少个,以检查用户得到的分数。

然后我第二天尝试做到这一点,如果您没有输入指定的答案之一,程序会让您再次输入答案,并输出一条消息,要求您再次输入答案。

我对编程还很陌生,所以如果有任何帮助或批评,我们将不胜感激。

import time

print("Welcome! This is the special IQ quiz...")
time.sleep(2)
print("You start at 60 IQ and you get 20 IQ for each question you get correct...")
time.sleep(2)
print("Let's see how smart you are, good luck!\n")
time.sleep(1)

iq = []
qustn_answrd = []

while len(qustn_answrd) <= 7:
    qustn1 = input("What's the name of the river that runs through Egypt?\n>> The Mississippi River\n>> The Nile\n>> The Yangtze River\n>> The Amazon River\n")
    if qustn1 == "The Nile":
        iq.append(1)
        qustn_answrd.append(1)
    elif qustn1 == "The Mississippi River" or "The Yangtze River" or "The Amazon River":
        qustn_answrd.append(1)
    elif qustn1 != "The Nile" or "The Mississippi River" or "The Yangtze River" or "The Amazon River":
        print("That is not a valid answer, make sure to input one of\nthe answers and if you did, make sure its written how it is displayed in the question...")
    else:
        break

while len(qustn_answrd) <= 7:
    qustn2 = input("Who was the Prime Minister before Theresa May?\n>> John Major\n>> Tony Blair\n>> David Cameron\n>> Harold Wilson\n")
    if qustn2 == "David Cameron":
        iq.append(1)
        qustn_answrd.append(1)
    elif qustn2 == "John Mayor" or "Tony Blair" or "Harold Wilson":
        qustn_answrd.append(1)
    else:
        print("That is not a valid answer, make sure to input one of\nthe answers and if you did, make sure its written how it is displayed in the question...")
        break

while len(qustn_answrd) <= 7:
    qustn3 = input("Who did Orlando Bloom play in the Pirates In The Caribbean?\n>> Captain Jack Sparrow\n>> Hector Barbossa\n>> Davy Jones\n>> Will Turner\n")
    if qustn3 == "Will Turner":
        iq.append(1)
        qustn_answrd.append(1)
    elif qustn3 == "Captain Jack Sparrow" or "Hector Barbossa" or "Davy Jones":
        qustn_answrd.append(1)
    else:
        print("That is not a valid answer, make sure to input one of\nthe answers and if you did, make sure its written how it is displayed in the question...")
        break

while len(qustn_answrd) <= 7:
    qustn4 = input("What's the highest mountain in the world?\n>> Mount Everest\n>> K2\n>> Kangchenjunga\n>> Lhotse\n")
    if qustn4 == "Mount Everest":
        iq.append(1)
        qustn_answrd.append(1)
    elif qustn4 == "K2" or "Kangchenjunga" or "Lhotse":
        qustn_answrd.append(1)
    else:
        print("That is not a valid answer, make sure to input one of\nthe answers and if you did, make sure its written how it is displayed in the question...")
        break

while len(qustn_answrd) <= 7:
    qustn5 = input("How many wives did Henry VIII have?\n>> Eight\n>> Six\n>> Seven\n>> Nine\n")
    if qustn5 == "Six":
        iq.append(1)
        qustn_answrd.append(1)
    elif qustn5 == "Eight" or "Seven" or "Nine":
        qustn_answrd.append(1)
    else:
        print("That is not a valid answer, make sure to input one of\nthe answers and if you did, make sure its written how it is displayed in the question...")
        break

while len(qustn_answrd) <= 7:
    qustn6 = input("What is the capital of Spain\n>> Madrid\n>> Barcelona\n>> Seville\n>> Granada\n")
    if qustn6 == "Madrid":
        iq.append(1)
        qustn_answrd.append(1)
    elif qustn6 == "Barcelona" or "Seville" or "Granada":
        qustn_answrd.append(1)
    else:
        print("That is not a valid answer, make sure to input one of\nthe answers and if you did, make sure its written how it is displayed in the question...")
        break

while len(qustn_answrd) <= 7:
    qustn7 = input("Who is the highest selling fiction author of all time?\n>> Barbara Cartland\n>> William Shakespeare\n>> Agatha Christie\n>> Danielle Steel\n")
    if qustn7 == "Agatha Christie":
        iq.append(1)
        qustn_answrd.append(1)
    elif qustn7 == "Barbara Cartland" or "William Shakespeare" or "Danielle Steel":
        qustn_answrd.append(1)
    else:
        print("That is not a valid answer, make sure to input one of\nthe answers and if you did, make sure its written how it is displayed in the question...")
        break

if len(iq) == 0:
    print("You answered none of the questions correctly so you have an IQ of 60")
if len(iq) == 1:
    print("You answered 1 out of 7 questions correctly so you have an IQ of 80")
if len(iq) == 2:
    print("You answered 2 out of 7 questions correctly so you have an IQ of 100")
if len(iq) == 3:
    print("You answered 3 out of 7 questions correctly so you have an IQ of 120")
if len(iq) == 4:
    print("You answered 4 out of 7 questions correctly so you have an IQ of 140")
if len(iq) == 5:
    print("You answered 5 out of 7 questions correctly so you have an IQ of 160")
if len(iq) == 6:
    print("You answered 6 out of 7 questions correctly so you have an IQ of 180")
if len(iq) == 7:
    print("You answered all of the questions correctly so you have an IQ of 200")

你可以这样做 -

pass = True
while pass:
    qustn1 = input("What's the name of the river that runs through Egypt?\n>> The Mississippi River\n>> The Nile\n>> The Yangtze River\n>> The Amazon River\n")
    if qustn1 == "The Nile":
        iq.append(1)
        qustn_answrd.append(1)
        pass = False
    elif qustn1 == "The Mississippi River" or "The Yangtze River" or "The Amazon River":
        qustn_answrd.append(1)
        pass = False
    elif qustn1 != "The Nile" or "The Mississippi River" or "The Yangtze River" or "The Amazon River":
        print("That is not a valid answer, make sure to input one of\nthe answers and if you did, make sure its written how it is displayed in the question...")
        pass = False
    else:
        pass = True

所以你做 bool of pass 并检查它是真还是假

首先,最好将所有问题和答案与逻辑分开。

其次,您可以通过遍历问题列表来避免每个问题的代码重复。

第三,使用while True循环不断问同样的问题。

QUESTIONS = [{"Question": "What's the name of the river that runs through Egypt?",
              "Answers": ["The Nile", "The Mississippi River", "The Yangtze River", "The Amazon River"],
              "CorrectAnswer": "The Nile"},
             {"Question": "Who was the Prime Minister before Theresa May?",
              "Answers": ["David Cameron", "John Mayor", "Tony Blair", "Harold Wilson"],
              "CorrectAnswer": "David Cameron"}]

score = 0
for question in QUESTIONS:
    while True:
        answer = input(question['Question'] + '\n>> ' + '\n>> '.join(question['Answers']) + '\n')
        if answer in question['Answers']:
            if answer == question['CorrectAnswer']:
                score += 1
            break
        else:
            print('That is not a valid answer, make sure to input one of\nthe answers and if you did, make sure its written how it is displayed in the question...')

iq = score * 20 + 60
if score == 0:
    print(f'You answered none of the questions correctly so you have an IQ of {iq}')
elif 0 < score < len(QUESTIONS):
    print(f'You answered {score} out of {len(QUESTIONS)} questions correctly so you have an IQ of {iq}')
elif score == len(QUESTIONS):
    print(f'You answered all of the questions correctly so you have an IQ of {iq}')

示例输出:

What's the name of the river that runs through Egypt?
>> The Nile
>> The Mississippi River
>> The Yangtze River
>> The Amazon River
The Nile
Who was the Prime Minister before Theresa May?
>> David Cameron
>> John Mayor
>> Tony Blair
>> Harold Wilson
John Mayor
You answered 1 out of 2 questions correctly so you have an IQ of 80