简单测验 - 如何链接变量?

时间:2016-10-03 18:10:16

标签: python

我一直试图弄清楚如何将正确的答案与正确的问题相匹配。现在,如果用户的答案等于任何答案,则返回正确。请帮忙。

easy_question = "The capitol of West Virginia is __1__"
medium_question = "The device amplifies a signal is an __2__"
hard_question = "A program takes in __3__ and produces output."

easy_answer = "Charleston"
medium_answer = "amplifier"
hard_answer = "input"

questions_and_answers = {easy_question: easy_answer, 
                    medium_question: medium_answer,
                    hard_question: hard_answer}
#print(easy_answer in [easy_question, easy_answer])
#print(questions_and_answers[0][1])

print('This is a quiz')
ready = input("Are you ready? Type Yes.")
while ready != "Yes":
    ready = input("Type Yes.")
user_input = input("Choose a difficulty: Easy, Medium, or Hard")

def choose_difficulty(user_input):
    if user_input == "Easy":
        return easy_question
    elif user_input == "Medium":
        return medium_question
    elif user_input == "Hard":
        return hard_question
    else:
        print("Incorrect")
        user_input = input("Type Easy, Medium, or Hard")
print(choose_difficulty(user_input))


answer = input("What is your answer?")
def check_answer(answer):
    if answer == easy_answer:
        return "Correct"
    elif answer == medium_answer:
        return "Correct"
    elif answer == hard_answer:
        return "Correct"
 print(check_answer(answer))

2 个答案:

答案 0 :(得分:1)

您需要跟踪question

question = choose_difficulty(user_input)
print(question)

answer = input("What is your answer?")
def check_answer(question, answer):
    if questions_and_answers[question] == answer:
        return "Correct"
    return "Incorrect"
print(check_answer(question, answer))

你可以做很多很酷的事情,但这是一个可以解决问题的最小例子!

修改

当你做了

questions_and_answers = {easy_question: easy_answer, 
                medium_question: medium_answer,
                hard_question: hard_answer}

你创建了一个字典(或者在Python中已知的dict)。见examples。基本上,你可以按第一个词(问题)进行查找,然后返回第二个词(答案)。

答案 1 :(得分:-1)

我会这样做:创建2个变量,x和y。如果用户选择“Easy”,则将x设置为1,“Medium”将其设置为2,依此类推。然后你问他答案。简单问题的答案,如果正确,将y设置为1,将介质设置为2,依此类推。然后检查x == y。如果是,那么他已正确回答了问题。