2017-08-14 1 views
0

Ich schreibe ein Programm, das eine fünf Fragen multiple- Choice-Quiz auf die globale Erwärmung verwaltet und berechnet die Nummer der richtigen Antworten. habe ich ein Wörterbuch der Wörterbücher zuerst, wie:Multiple-Choice-Quiz mit Python

questions = \ 
{ 
    "What is the global warming controversy about?": { 
     "A": "the public debate over whether global warming is occuring", 
     "B": "how much global warming has occured in modern times", 
     "C": "what global warming has caused", 
     "D": "all of the above" 
    }, 
    "What movie was used to publicize the controversial issue of global warming?": { 
     "A": "the bitter truth", 
     "B": "destruction of mankind", 
     "C": "the inconvenient truth", 
     "D": "the depletion" 
    }, 
    "In what year did former Vice President Al Gore and a U.N. network of scientists share the Nobel Peace Prize?": { 
     "A": "1996", 
     "B": "1998", 
     "C": "2003", 
     "D": "2007" 
    }, 
    "Many European countries took action to reduce greenhouse gas before what year?": { 
     "A": "1985", 
     "B": "1990", 
     "C": "1759", 
     "D": "2000" 
    }, 
    "Who first proposed the theory that increases in greenhouse gas would lead to an increase in temperature?": { 
     "A": "Svante Arrhenius", 
     "B": "Niccolo Machiavelli", 
     "C": "Jared Bayless", 
     "D": "Jacob Thornton" 
    } 
} 

Dann wird die Logik ist wie folgt:

print("\nGlobal Warming Facts Quiz") 
prompt = ">>> " 
correct_options = ['D', 'C', 'D', 'B', 'A'] 
score_count = 0 

for question, options in questions.items(): 
    print("\n", question, "\n") 
    for option, answer in options.items(): 
     print(option, ":", answer) 
    print("\nWhat's your answer?") 
    choice = str(input(prompt)) 
    for correct_option in correct_options: 
     if choice.upper() == correct_option: 
      score_count += 1 
print(score_count) 

Das Problem ist, wenn ich alle richtigen Optionen eingeben, bekomme ich 7 statt 5 I versuchte, die letzte Aussage (print (score_count)) unter der wenn Anweisung, um die Punktzahl zu überwachen und ich dachte, dass einige Fragen tatsächlich 1 zweimal statt nur einmal hinzufügen.

Antwort

4

Es ist wegen der Tatsache, dass Sie für jede Frage alle richtigen Optionen für alle Fragen iterieren, anstatt zu prüfen, ob die angegebene Option nur der richtigen Option für die aktuelle Frage entspricht. Mit anderen Worten, dies ist Teil falsch:

for correct_option in correct_options: 
     if choice.upper() == correct_option: 
      score_count = score_count + 1 

Versuchen Sie stattdessen:

print("\nGlobal Warming Facts Quiz") 
prompt = ">>> " 
correct_options = ['D', 'C', 'D', 'B', 'A'] 
score_count = 0 

for correct_option, (question, options) in zip(correct_options, questions.items()): 
    print("\n", question, "\n") 
    for option, answer in options.items(): 
     print(option, ":", answer) 
    print("\nWhat's your answer?") 
    choice = str(input(prompt)) 
    if choice.upper() == correct_option: 
     score_count = score_count + 1 
print(score_count)