【问题标题】:How do I get Python to only allow a specific answer如何让 Python 只允许特定答案
【发布时间】:2021-05-02 10:47:11
【问题描述】:

你好,我需要帮助我一直在做的这个程序,这是一个会问用户琐事问题然后计算用户智商的程序,我设法让程序问用户问题,然后添加一个 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")

【问题讨论】:

  • 您是否遇到过具体问题?我没听懂你的问题是什么。

标签: python input while-loop break


【解决方案1】:

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

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

第三,使用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

【讨论】:

  • 非常感谢您的回答,这肯定向我展示了我做错了什么,我将使用它作为将来压缩我的代码的参考。非常感谢!!
【解决方案2】:

你可以这样做 -

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 并检查它是真还是假

【讨论】:

  • 您好,谢谢您的回答,我尝试将其复制到 PyCharm 中,但在等号下出现一堆错误,这可能是我的 IDE 错误。我真的很想让这段代码工作,因为它看起来和我的很相似,也是我想要的。谢谢你的帮助!!!
  • 它说'pass = true'中的'='是SyntaxError: invalid syntax
  • 哦,对不起,正确的,而不是 true 写入 True,而 false 写入 False,我忘记了,我编辑了我的答案。
猜你喜欢
  • 1970-01-01
  • 2012-06-03
  • 1970-01-01
  • 2016-07-20
  • 2011-12-13
  • 2021-03-20
  • 2017-11-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多