0

So i want to be able to make a multiple choice quiz program using dictionaries. I have one dictionary with all the questions as the key and the answer as a value and a second dictionary thats empty. I want to append all of the incorrect questions someone may have into the empty dictionary. i want to do this in order to allow users to retake the exam but only with the questions that they answered wrong. Yet i cannot find a way to append a key and value from one list to another without being specific.

Here is my code below:

def intro():    
    print("Hello and welcome to the random questions test")
    global name   
    name = input("what is your name? " + "\n" + "\n")
    if name == "":
        print("Enter a valid name!")
        name = input("what is your name? " + "\n" + "\n")
    else:
        ans2 = input("\n" + "Hello " + name + " Are you sure you want to take the exam? (yes/no)"  + "\n" + "\n")
        if ans2 == "no":
            print("\n" + "aw man, until next time " + name + "!" + "\n")   
        elif ans2 == "yes":
            print("\n" + "Okay!" + "\n")
            Quiz()

def Quiz():
    q1 = """Who holds the NFL record for the most receiving yards in a single game?
        a.  Calvin Johnson
        b.  Deandre Hopkins
        c.  Flipper Anderson
        d.  Jerry Rice"""
    q2 = """In what year was the eiffel tower built?
        a.  1492
        b.  1889
        c.  2013
        d.  1766"""
    q3 = """How many countries are in the world today?
        a.  205
        b.  195
        c.  673
        d.  142"""
    q4 = """Who is the CEO of apple currently (2020)?
        a.  Tim Cook
        b.  Sergio Kitchens
        c.  Daniel Johnson
        d.  Steve Jobs"""
    q5 = """who is the 23rd president of the united states?
        a.  Grover Cleveland 
        b.  Andrew Jackson
        c.  Anderson Berkshire
        d.  Benjamin Harrison"""
    q6 = """X + 5x - 10 = 500... What does X equal?
        a.  85
        b.  84
        c.  102
        d.  0"""
    q7 = """Who is currently the richest person on earth?
        a.  Tim Cook
        b.  Jeff Bezos
        c.  Bill Gates
        d.  Bill Nye"""
    q8 = """When was Howard University Established?
        a.  May 4, 2002
        b.  March 5, 1855
        c.  September 1, 1844
        d.  March 2, 1867"""
    q9 = """What can you catch but not throw?
        a.  A football
        b.  A conversation
        c.  A cold
        d.  A tennis ball in the Appalachian Mountains"""
    q10 = """Which country is responsible for the production of 95% of the worlds opal stones?
        a.  Brazil 
        b.  Mexico
        c.  Honduras
        d.  Austrailia"""
    q11 = """How many species of Toothed and Baleen whales exist?
        a.  14 and 2
        b.  76 and 14
        c.  5 and 10
        d.  75,000"""
    q12 = """Who holds the world record for the most chicken nuggets eaten in 1 hour?
        a.  Naader Reda
        b.  Nicolo Reda
        c.  Stefano Neder
        d.  Josh Jacobs"""
    q13 = """Who invented the bessemer process?
        a.  Henry Bessemer 
        b.  Markus Bessemer
        c.  Bessemer Robinson
        d.  Anakin Skywalker"""
    q14 = """When was Mortal Kombat 6 released?
        a.  April 1, 2002
        b.  August 15, 2007
        c.  October 4, 2004
        d.  December 25, 2006"""
    q15 = """In which City located in the USA is it illegal to say "Oh, boy"?
        a.  Denver, Colorado  
        b.  Lexington, Kentucky
        c.  Jonesboro, Georgia
        d.  Wilbur, Washington"""
    Questions = {q1: 'c', q2: 'b', q3: 'b', q4: 'a', q5: 'd', q6: 'a', q7: 'b', q8: 'd', q9: 'c', q10: 'd', q11: 'b', q12: 'a', q13: 'a', q14: 'c', q15: 'c' }
    inc_questions = {}
    score = 0
    for q in Questions:
        print(q)
        ans = input("> ")
        if ans.lower() == Questions[q]:
            print("\n" + "Correct!" + "\n")
            score += 1
        else:
            print("\n" + "Incorrect!" + "\n")
            for q, a in Questions.items():
                if ans.lower() != Questions[q]:
                    inc_questions[q] = a 
   
    if score == 15: 
        print("Congratulations " + name + ", you scored a perfect 15/15!")
    elif score < 15 and score > 11:
        print("Good job " + name + "! you got " + str(score) + " out of 15 correct!")
        print("\n" + "Would you like to re-answer the questions that were incorrect?" + "\n")
    elif score < 12 and score > 8:
        print("You did pretty well " + name + ", you got " + str(score) + " out of 15 correct!")
        print("\n" + "Would you like to re-answer the questions that were incorrect?" + "\n")
    elif score < 9 and score > 5:
        print("Unfortunately " + name + " you did not pass, you got " + str(score) + " out of 15 correct!")
        print("\n" + "Would you like to re-answer the questions that were incorrect?" + "\n")
    elif score < 6 and score > 0:
        print("Jeez " + name + " you did awful lol, you got " + str(score) + " out of 15 correct!")
        print("\n" + "Would you like to re-answer the questions that were incorrect?" + "\n")
    elif score == 0:
        print("Oh lord " + name +" you need some serious help you got nothing right!")
        print("\n" + "Would you like a retake?" + "\n")
    print(inc_questions)

intro()
wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • 1
    I think I would redefine your initial dictionary with keys ("q1", "q2", . . . "q15") the dictionary value can then be a tuple ("Question text", "correct answer value". The you can cycle through the dictionary based on key, value pair.and ask ask your question with dict[key][0] and comare answer to dict[kay][1]. If answer is correct, update answer count, if answer is incorrect store the question key in a re-quiz list, so only questions with incorrect answers will be re asked. – itprorh66 Nov 21 '20 at 00:23
  • https://realpython.com/python-dicts/ – Dave Nov 21 '20 at 00:24
  • A Dictionary is a key->value pair, by definition. If you want to append you might be better off with a List. You could also use an ordered dict, if you really want to be able to "append" https://stackoverflow.com/questions/10058140/accessing-items-in-an-collections-ordereddict-by-index .... however, I still don't understand why you want to "append" to your dictionary - both lists and dicts are iterable. – Tim Nov 21 '20 at 02:19
  • 1
    Welcome to SO! Check out the [tour]. This is way too much code. Please make a [mre]. You can [edit] the question. Check out [ask] if you want more tips. – wjandrea Nov 21 '20 at 04:41
  • Based on my answer, the question title does not match. I am staying away from the key value pair and instead using tuples inside a list to get this working. This is more of a list solution than a dict solution. The reason for list vs. dict is to keep track of unanswered questions and iterate thru them quickly. – Joe Ferndz Nov 21 '20 at 06:49

1 Answers1

1

Interesting problem to solve. Look at this code and see if it provides you the repeatable process to keep continuing with your quiz. The only area that I have a bit of a problem is your big if statements that check for scores and print varying responses. When the user has fewer questions, I had to add the older answered questions to the tally to stay in the same range. Otherwise, this should work.

Things I changed.

#1: Questions is a list of tuples. Each tuple is a question and answer (q1,'c') as example.

#2: Since we need to repeat the questions, I am iterating through incorrect question list each time. To start off, I set all questions as incorrect. So the incorrect questions list has values 0 thru 14.

#3: Every time the user answers correctly, I am removing the question from the incorrect question list.

#4: Since I am manipulating the list itself by removing the correctly answered question, I cannot use a for loop. Instead I am using a while loop and ensuring I am going through the list only till the max of list

#5: I am looping the Quiz function until the user decides to stop playing. To start with, I am setting the flag as yes and checking for it before I call Quiz function. I am returning the user's decision back as a return statement. That is helping the loop to keep going.

#6: Finally, I moved all the questions outside and made Questions a global variable. Since we are going to call Quiz a few times, I didn't want Questions to be defined every time. If you want to keep it inside, its your choice. It does not impact the overall solution. However, you need to make it a list of tuples. Additionally, inc_questions has to be global so you can manipulate it as many times as you need.

Below is the code. Let me know if you find any errors.

def intro():    
    print("Hello and welcome to the random questions test")
    global name   
    name = input("what is your name? " + "\n" + "\n")
    if name == "":
        print("Enter a valid name!")
        name = input("what is your name? " + "\n" + "\n")
    else:
        ans2 = input("\n" + "Hello " + name + " Are you sure you want to take the exam? (yes/no)"  + "\n" + "\n")
        if ans2.lower() == "no":
            print("\n" + "aw man, until next time " + name + "!" + "\n")   
        elif ans2.lower() == "yes":
            print("\n" + "Okay!" + "\n")
            ans3 = 'yes'
            while ans3.lower() == 'yes':
                ans3 = Quiz()

def Quiz():
    score = 15 - len(inc_questions)
    i = 0
    while i < len(inc_questions):
        x = inc_questions[i]
        print(Questions[x][0])
        ans = input("> ")
        if ans.lower() == Questions[x][1]:
            print("\n" + "Correct!" + "\n")
            score += 1
            inc_questions.pop(i)
        else:
            print("\n" + "Incorrect!" + "\n")
            i +=1
   
    if score == 15: 
        print("Congratulations " + name + ", you scored a perfect 15/15!")
    elif score < 15 and score > 11:
        print("Good job " + name + "! you got " + str(score) + " out of 15 correct!")
    elif score < 12 and score > 8:
        print("You did pretty well " + name + ", you got " + str(score) + " out of 15 correct!")
    elif score < 9 and score > 5:
        print("Unfortunately " + name + " you did not pass, you got " + str(score) + " out of 15 correct!")
    elif score < 6 and score > 0:
        print("Jeez " + name + " you did awful lol, you got " + str(score) + " out of 15 correct!")
    elif score == 0:
        print("Oh lord " + name +" you need some serious help you got nothing right!")

    if score == 0:
        print("\n" + "Would you like a retake?" + "\n")
        ans1 = input("> ")
    elif score < 15:
        print("\n" + "Would you like to retake the questions that were incorrect?" + "\n")
        ans1 = input("> ")
    else: ans1 = ''
    
    return ans1

q1 = """Who holds the NFL record for the most receiving yards in a single game?
        a.  Calvin Johnson
        b.  Deandre Hopkins
        c.  Flipper Anderson
        d.  Jerry Rice"""
q2 = """In what year was the eiffel tower built?
        a.  1492
        b.  1889
        c.  2013
        d.  1766"""
q3 = """How many countries are in the world today?
        a.  205
        b.  195
        c.  673
        d.  142"""
q4 = """Who is the CEO of apple currently (2020)?
        a.  Tim Cook
        b.  Sergio Kitchens
        c.  Daniel Johnson
        d.  Steve Jobs"""
q5 = """who is the 23rd president of the united states?
        a.  Grover Cleveland 
        b.  Andrew Jackson
        c.  Anderson Berkshire
        d.  Benjamin Harrison"""
q6 = """X + 5x - 10 = 500... What does X equal?
        a.  85
        b.  84
        c.  102
        d.  0"""
q7 = """Who is currently the richest person on earth?
        a.  Tim Cook
        b.  Jeff Bezos
        c.  Bill Gates
        d.  Bill Nye"""
q8 = """When was Howard University Established?
        a.  May 4, 2002
        b.  March 5, 1855
        c.  September 1, 1844
        d.  March 2, 1867"""
q9 = """What can you catch but not throw?
        a.  A football
        b.  A conversation
        c.  A cold
        d.  A tennis ball in the Appalachian Mountains"""
q10 = """Which country is responsible for the production of 95% of the worlds opal stones?
        a.  Brazil 
        b.  Mexico
        c.  Honduras
        d.  Austrailia"""
q11 = """How many species of Toothed and Baleen whales exist?
        a.  14 and 2
        b.  76 and 14
        c.  5 and 10
        d.  75,000"""
q12 = """Who holds the world record for the most chicken nuggets eaten in 1 hour?
        a.  Naader Reda
        b.  Nicolo Reda
        c.  Stefano Neder
        d.  Josh Jacobs"""
q13 = """Who invented the bessemer process?
        a.  Henry Bessemer 
        b.  Markus Bessemer
        c.  Bessemer Robinson
        d.  Anakin Skywalker"""
q14 = """When was Mortal Kombat 6 released?
        a.  April 1, 2002
        b.  August 15, 2007
        c.  October 4, 2004
        d.  December 25, 2006"""
q15 = """In which City located in the USA is it illegal to say "Oh, boy"?
        a.  Denver, Colorado  
        b.  Lexington, Kentucky
        c.  Jonesboro, Georgia
        d.  Wilbur, Washington"""

Questions = [(q1,'c'),  (q2,'b'),  (q3,'b'), (q4,'a'),  (q5,'d'),  (q6,'a'),
             (q7,'b'),  (q8,'d'),  (q9,'c'), (q10,'d'), (q11,'b'), (q12,'a'),
             (q13,'a'), (q14,'c'), (q15,'c')]

inc_questions = [i for i in range(15)]

intro()

Here's the output part that goes back to the questions again.

I got the following questions wrong: index value of [0, 3, 4, 6, 7, 9, 11, 12, 13, 14]

Here's the 15th question. Then it goes and asks the 1st and 4th question.

In which City located in the USA is it illegal to say "Oh, boy"?
        a.  Denver, Colorado  
        b.  Lexington, Kentucky
        c.  Jonesboro, Georgia
        d.  Wilbur, Washington
> a

Incorrect!

Jeez Joe you did awful lol, you got 5 out of 15 correct!

Would you like to retake the questions that were incorrect?

> yes
Who holds the NFL record for the most receiving yards in a single game?
        a.  Calvin Johnson
        b.  Deandre Hopkins
        c.  Flipper Anderson
        d.  Jerry Rice
> a

Incorrect!

Who is the CEO of apple currently (2020)?
        a.  Tim Cook
        b.  Sergio Kitchens
        c.  Daniel Johnson
        d.  Steve Jobs
> a

Correct!

who is the 23rd president of the united states?
        a.  Grover Cleveland 
        b.  Andrew Jackson
        c.  Anderson Berkshire
        d.  Benjamin Harrison
> a

Incorrect!

Who is currently the richest person on earth?
        a.  Tim Cook
        b.  Jeff Bezos
        c.  Bill Gates
        d.  Bill Nye
> 

Then it goes back and asks again. From the previous round, my 15th question was incorrect. So its asking that final question again.

When was Mortal Kombat 6 released?
        a.  April 1, 2002
        b.  August 15, 2007
        c.  October 4, 2004
        d.  December 25, 2006
> c

Correct!

In which City located in the USA is it illegal to say "Oh, boy"?
        a.  Denver, Colorado  
        b.  Lexington, Kentucky
        c.  Jonesboro, Georgia
        d.  Wilbur, Washington
> b

Incorrect!

You did pretty well Joe, you got 10 out of 15 correct!

Would you like to retake the questions that were incorrect?

> no
>>> 

I missed these questions again [0, 4, 7, 12, 14]. This time around, I said no and the program ended.

Joe Ferndz
  • 8,417
  • 2
  • 13
  • 33