我一直在試圖弄清楚如何將正確的答案與正確的問題進行匹配。現在,如果用戶的答案等於任何答案,則返回正確。請幫忙。簡單測驗 - 如何鏈接變量?
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))
你還沒有嘗試在'check_answer'中返回除''Correct''之外的任何東西。 – TigerhawkT3
因爲'if/elif'將會繼續檢查條件,直到事情成立,然後你將遇到'return'條件。在'check_answer'中,你只要繼續下去,直到找到正確的東西。 – roganjosh