1
from easygui import *
from time import *
from statedict import *
import random

correctnum = 0
questionsanswered = 0
print("Indian Map Quiz v1")
sleep(0.5)
print("Note all features might not work correctly, this is a work in progress.")


msgbox("Welcome to Indian Map Quiz", "INDIA", "Continue")

title = "Guess The State"
msg = "What Indian state is this?"

stateFactList = [APdict, ARdict, ASdict, BRdict, CTdict, GAdict, GJdict, HPdict, HRdict, JHdict,
                 KAdict, KLdict, MHdict, MLdict, MNdict, MPdict, MZdict, NLdict, ODdict, PBdict,
                 RJdict, SKdict, TGdict, TNdict, TRdict, UPdict, UTdict, WBdict]




stateID = random.choice(stateFactList)
print(stateID["state"])
stateQuestion = buttonbox(msg=msg, title=title, choices=stateID["choices"], image=stateID["image file"])

if stateQuestion == stateID["correct"]:
    print("it's correct")
    correctnum += 1
    questionsanswered += 1
else:
    print("incorrect")
    questionsanswered += 1

Here is the code, essentially when you run the program, it should randomly pick a state and provide some multiple choice answers based on the state. It randomly picks a state from the stateFactlist list and matches it with a dictionary stored in another file. Whenever the user answers a question, I want it to generate a new, random state to be displayed to the user, along with the respective multiple choice answers, but I can't find a way to implement it. Help is appreciated.

  • 3
    Does this answer your question? [Random without repetition?](https://stackoverflow.com/questions/30735892/random-without-repetition) – mkrieger1 Jan 16 '23 at 19:59
  • hint: in some variable you can track already picked from the list, or better remove picked element from the list – Hackaholic Jan 16 '23 at 20:01
  • @adrianop01 random.sample() will give unique elemnts from the list but when you call again on list it might give repetations – Hackaholic Jan 16 '23 at 20:03
  • @Hackaholic what do you mean by "might give repetitions"? There is a limited number of possible permutations for any finite list, running ANY sampling algorithms to generate a permutation of a finite list will eventually encounter repetitions – adrianop01 Jan 16 '23 at 20:20

3 Answers3

3

To help clear up the confusion, random.sample() includes a parameter, k, which lets you specify the number of unique elements to randomly choose from the specified sequence. It can work well for what OP has in mind. Here is a simplified example for illustration purposes:

import random

arr = ["A", "B", "C", "D"]
for x in random.sample(arr, k=len(arr)):
    print(x)

Output:

C
D
A
B

EDIT: In response to OP's question on how to implement this in their code, here is a rough approximation. The key is to move the state Q&A code inside of a loop that is iterating (in random order) over stateFactList. Note, I wasn't able to run this code since I don't have access to OP's data structures or GUI library, so take it as a rough guide, not working code.

stateFactList = [APdict, ARdict, ASdict, BRdict, CTdict, GAdict, GJdict, HPdict, HRdict, JHdict,
                 KAdict, KLdict, MHdict, MLdict, MNdict, MPdict, MZdict, NLdict, ODdict, PBdict,
                 RJdict, SKdict, TGdict, TNdict, TRdict, UPdict, UTdict, WBdict]

for stateID in random.sample(stateFactList, k=len(stateFactList)):  # iterate over stateFactList in random order 

    msg = "What Indian state is this?"

    # This statement no longer needed
    # stateID = random.choice(stateFactList)
    
    print(stateID["state"])
    stateQuestion = buttonbox(msg=msg, title=title, choices=stateID["choices"], image=stateID["image file"])

    if stateQuestion == stateID["correct"]:
        print("it's correct")
        correctnum += 1
        questionsanswered += 1
    else:
        print("incorrect")
        questionsanswered += 1
rhurwitz
  • 2,557
  • 2
  • 10
  • 18
2

Just shuffle the list, then iterate normally.

randome.shuffle(stateFactList)
for state in stateFactList:
    ...
chepner
  • 497,756
  • 71
  • 530
  • 681
0

you can remove the already picked element from the list for fair share

Like below example:

>>> a = [1,2,3,4,5]
>>> choice = random.choice(a)
>>> choice
4
>>> a.remove(a.index(choice))     # removes 4 from the list
>>> a
[1, 2, 4, 5]
>>> choice = random.choice(a)
>>> choice
2
Hackaholic
  • 19,069
  • 5
  • 54
  • 72