A quiz using random module and list

A quiz using random module and list
0

#1

‘’‘Write a simple quiz game that has a list of ten questions and a list of answers to those questions.
The game should give the player four randomly selected questions to answer. It should
ask the questions one-by-one, and tell the player whether they got the question right or
wrong. At the end it should print out how many out of four they got right.’’’
from random import *
Questions=
[
'What is the capital of the United States of America? ',
'Who is the current president of the United States of America? ',
'What is the capital of Nigeria? ',
'Muhammadu Buhari is the president of which country? ',
'Uhuru Kenyatta is the president of which country? ',
'What is the capital of France? ',
'Which state has only one neighbor? ',
'Where is Amsterdam located? ',
'Emmanuel Macron is the president of which country? ',
'Sir Alex Ferguson until his retirement coached which team? ’
]

Answers=[ ‘Washington DC’,
‘President Donald Trump’,
‘Abuja’
‘Nigeria’,
‘Kenya’,
‘Paris’,
‘Maine’,
‘Netherland’,
‘France’,
‘Manchester United’
]

num_right= 0
s = sample(Questions, 4)
for i in range(len(s)):
answer= input(Questions[i])
if answer.lower()==Answers[i].lower():
print(‘Correct!!!’)
num_right+=1
else:
print('Not correct ')


#2

The program is not running correctly as it should run, it is printing out the 4 questions but it is not giving the right answers.


#3

So first and foremost, the reason answers are likely not matching questions is because there is a comma missing in the answers list after 'Abuja' so the answers list is one item shorter than the questions list.

I’m guessing this is just a copy-paste issue, but just in case. Strings need to have consistent start and end indicators '' or "". There’s some other types of apostrophes going on in the Answer/Questions list.

Finally, in the loop: for i in range(len(s)):
If we made a list of all i's it would look like [0, 1, 2, 3] in that order. So i will most likely not actually match the correct index of Answers, if it does it’s just random chance. There’s a few ways around this but I find the simplest way is to create a new list with the questions and answers side-by-side in a tuple. This can be done with the zip() function (so long as the order of either list isn’t messed with before we zip the lists together).

Here is the code that seems to work for me:

# I avoid * imports most of the time
# Plus there's no sense importing the whole module if you only need one function from it
from random import sample

Questions = ['What is the capital of the United States of America? ',
             'Who is the current president of the United States of America? ',
             'What is the capital of Nigeria? ',
             'Muhammadu Buhari is the president of which country? ',
             'Uhuru Kenyatta is the president of which country? ',
             'What is the capital of France? ',
             'Which state has only one neighbor? ',
             'Where is Amsterdam located? ',
             'Emmanuel Macron is the president of which country? ',
             'Sir Alex Ferguson until his retirement coached which team? '
             ]

Answers = ['Washington DC',
           'President Donald Trump',
           'Abuja',  # <-- comma added
           'Nigeria',
           'Kenya',
           'Paris',
           'Maine',
           'Netherland',
           'France',
           'Manchester United'
           ]

num_right = 0

# Personal taste, I prefer the zipped list of Q & A rather than an index lookup in the for loop
# Ex: key[0] == ('What is the capital of the United States of America? ', 'Washington DC')
# Final note, the zip function returns a generator, so we need to transform it to a list object for sample()
key = list(zip(Questions, Answers))

# Sample from key now instead of Questions
s = sample(key, 4)

for i in s:
    # in each "i" tuple [0] is the question, [1] is the answer
    print(i[0])
    user_answer = input('Your Guess: ')
    if user_answer.lower() == i[1].lower():
        print('Correct!!!')
        num_right += 1
    else:
        print('Not Correct')
# Added a final fraction of correct statement
print('{}/4 Questions Correct'.format(num_right))