2

I am wanting to use random.randint() to make a random quiz using list. As of now my code works well but there are times where it will ask the user a question twice instead of randomising all questions. This happens only sometimes. How do I avoid this by only using random.randint? I can only use this and .del() as this is what I am taught in my course.

     import random
print("Welcome to Te Reo Maori Quiz!!!")
print("\nAnswer the questions with single Maori words")
Q = ['A challenge laid down in chant and dance:',
             'What is "Sun" in Maori:',
             'Something you eat to fill your belly:',
             'Type in the Maori word for "cave":',
             'Traditional Maori food cooked in an earth oven:',
             'Ma is white, whero is red, kakariki is green, pango is black. What else is black?:',
             'its getting ... in here, so take off all your clothes:',
             'What does Kia ora mean?:',
             'What does ka pai mean?:',
             'What does kei te peha koe mean?:',
             'What is the Maori phrase for "what is your name?:',
             'What does hikoi mean?:',
             'What is a waiata:',
             'What is the the Maori word for stomach?:',
             'What does mahi mean?',
             'What is the maori word for wait?:',
             'if something was nui, then it would be what?:',
             'What does Haere mai mean? (hint: it starts with "w"):',
             'What does nau mai mean?:',
             'What does tangi mean?:',
             ]

A = ['haka', 'ra', 'kai', 'ana', 'hangi', 'mangu', 'wera', 'hello', 'good', 'how are you', 'ko wai to ingoa', 'walk',
'song', 'puku', 'work', 'taihoa', 'big', 'welcome', 'welcome', 'funeral' 
]


points = 0
current = 0 
quiz = 0 

while (quiz < 5):

    question = Q[current]
    answer = A[current]

    question = input("\nQ" + str(quiz + 1) + ". " + Q[current])

    if question.lower() == answer.lower(): 
        points = points + 20 #adds points if the answer is correct
        current = current + 2
        print("Correct Answer!") 

    else:
        print("Incorrect answer. The correct answer is:", A[current]) 
        ###points = points - 10
        ###current = current + 2
    #quiz = quiz + 1

    ###if points < 0:
        ###points = 0

print("\nEnd of Quiz.")
print("Your score: %", points, sep = "")

I expect my code to produce 5 random questions at every iteration.

  • 1
    Possible duplicate of [how to randomize order of questions in a quiz in python?](https://stackoverflow.com/questions/30970070/how-to-randomize-order-of-questions-in-a-quiz-in-python) – metatoaster Sep 05 '19 at 02:19
  • 1
    Possibly also similar to https://stackoverflow.com/questions/9755538/how-do-i-create-a-list-of-random-numbers-without-duplicates – DarrylG Sep 05 '19 at 02:24
  • put in an if statement to check if its choosing a question that's already been chosen and choose again if so? – Derek Eden Sep 05 '19 at 02:31
  • 1
    Possible duplicate of [How do I create a list of random numbers without duplicates?](https://stackoverflow.com/questions/9755538/how-do-i-create-a-list-of-random-numbers-without-duplicates) – milanbalazs Sep 05 '19 at 05:05
  • You should reduce the pool by the element you drew on each iteration, otherwise your routine will decrease in efficiency. Imagine drawing 99 elements from a list of 100 without repetition. If you draw from range(100) and then always check if you already drew this number it will take you more and more time to draw a new valid number. Do `samples = range(len(Q)); random.shuffle(samples)` and then whenever you draw a new element do `Q[samples.pop()]` - problem solved. – j-i-l Sep 05 '19 at 10:06

3 Answers3

1

Here is an example to avoid duplication in while loop.

Q = ['a', 'b', 'c', 'd', 'e']
while len(Q)!= 0:
    x = random.randint(0,len(Q)-1)
    print(Q[x])
    Q = [v for v in Q if v != Q[x]]

A only random.randint() allowed solution.

import random
print("Welcome to Te Reo Maori Quiz!!!")
print("\nAnswer the questions with single Maori words")
Q = ['A challenge laid down in chant and dance:',
             'What is "Sun" in Maori:',
             'Something you eat to fill your belly:',
             'Type in the Maori word for "cave":',
             'Traditional Maori food cooked in an earth oven:',
             'Ma is white, whero is red, kakariki is green, pango is black. What else is black?:',
             'its getting ... in here, so take off all your clothes:',
             'What does Kia ora mean?:',
             'What does ka pai mean?:',
             'What does kei te peha koe mean?:',
             'What is the Maori phrase for "what is your name?:',
             'What does hikoi mean?:',
             'What is a waiata:',
             'What is the the Maori word for stomach?:',
             'What does mahi mean?',
             'What is the maori word for wait?:',
             'if something was nui, then it would be what?:',
             'What does Haere mai mean? (hint: it starts with "w"):',
             'What does nau mai mean?:',
             'What does tangi mean?:',
             ]

A = ['haka', 'ra', 'kai', 'ana', 'hangi', 'mangu', 'wera', 'hello', 'good', 'how are you', 'ko wai to ingoa', 'walk',
'song', 'puku', 'work', 'taihoa', 'big', 'welcome', 'welcome', 'funeral' ]

CQ = Q[:]
points = 0
quiz = 0

while (quiz < 5):
    x = random.randint(0, len(CQ)-1)
    print(str(quiz + 1) + ". " + CQ[x])

    UA = input("\n " )
    if A[Q.index(CQ[x])].lower() == UA.lower():
        points = points + 20 #adds points if the answer is correct
        print("Correct Answer!") 

    else:
        print("Incorrect answer. The correct answer is:", A[Q.index(CQ[x])])        

    quiz += 1
    del CQ[x]


print("\nEnd of Quiz.")
print("Your score: ", points)

Yet this solution contains knowledge about:

  • a copy of a list CQ = Q[:]
  • find the index of an item in a list Q.index(CQ[x])
ComplicatedPhenomenon
  • 4,055
  • 2
  • 18
  • 45
0

Just shuffle your questions, and then iterate through them:

import random
Q = ['a', 'b', 'c', 'd', 'e']

random.shuffle(Q)
print(Q)
Christian Sloper
  • 7,440
  • 3
  • 15
  • 28
0

You should reduce the pool by the element you drew on each iteration, otherwise your routine will decrease in efficiency.

Imagine drawing 99 elements from a list of 100 without repetition. If you draw from range(100) every time and then check if you already drew this number before, it will take you more and more time to draw a new valid number.

Simply create a random sequence of indices:

import random
samples = range(len(Q))
random.shuffle(samples)

and then whenever you want a new random question, remove an element from the list of indices and get the corresponding question:

Q[samples.pop()]

You are sure to not draw any question twice and you do not alter the order of the list of questions. Problem solved.

j-i-l
  • 10,281
  • 3
  • 53
  • 70