Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

input - How do I get Python to only allow a specific answer

Hello I need help with this program I have been doing, this is a program which will ask the user trivia questions and then count the users IQ, I managed to make the program ask the user the questions and then add a 1 to the list that is named 'IQ' when the user answers the question correctly, I then had the computer count how many ones there are in the list to check the score that the user got.

I then the next day tried to make it so that the program would make you input the answer again if you didn't input one of the specified answers and output a message asking to input your answer again.

I am fairly new to programming so any help or criticism would be appreciated.

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!
")
time.sleep(1)

iq = []
qustn_answrd = []

while len(qustn_answrd) <= 7:
    qustn1 = input("What's the name of the river that runs through Egypt?
>> The Mississippi River
>> The Nile
>> The Yangtze River
>> The Amazon River
")
    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
the 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?
>> John Major
>> Tony Blair
>> David Cameron
>> Harold Wilson
")
    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
the 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?
>> Captain Jack Sparrow
>> Hector Barbossa
>> Davy Jones
>> Will Turner
")
    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
the 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?
>> Mount Everest
>> K2
>> Kangchenjunga
>> Lhotse
")
    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
the 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?
>> Eight
>> Six
>> Seven
>> Nine
")
    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
the 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
>> Madrid
>> Barcelona
>> Seville
>> Granada
")
    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
the 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?
>> Barbara Cartland
>> William Shakespeare
>> Agatha Christie
>> Danielle Steel
")
    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
the 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")
question from:https://stackoverflow.com/questions/65938828/how-do-i-get-python-to-only-allow-a-specific-answer

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

First, it's better to keep all your questions and answers separately from the logic.

Second, you can avoid code duplication for each question by iterating over questions list.

Third, use while True loop to keep asking same question.

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'] + '
>> ' + '
>> '.join(question['Answers']) + '
')
        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
the 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}')

Sample output:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...