0

I was wondering how I could fix a bug in my program. Basically, I have this short quiz program that's supposed to ask users a series of questions from a list they inputted previously (pickle loads these from a previous part of my code in the example below) in a randomized way, but the program will sometimes repeat the same question, which I don't want to happen.

I've looked on this site, but I could not understand the answers to questions similar to mine.

def quiz():
    import pickle
    import random
    import os
    os.system('cls')

    a = pickle.load(open("quiz.dat", "rb")))
    random.shuffle(a)
    #loads questions ans answers from previous section of program

    for b in a:
        print(b["question"])
        response = input("What was the answer? : ")
        if (response == b["answer"]):
            print("Good Answer!")
        else:
            print("Wrong answer...")
            print("The right answer was", b, ".")
    print("Quiz is now over.")
martineau
  • 119,623
  • 25
  • 170
  • 301
  • what is your problem? – mazhar islam Feb 03 '20 at 22:15
  • 1
    If you print out a immediately after `a = pickle.load(...` what does it look like? Are there repeated questions there? – Tom Dalton Feb 03 '20 at 22:19
  • 3
    Welcome to StackOverflow. See [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). We cannot effectively help you until you post your MRE code and accurately specify the problem. We should be able to paste your posted code into a text file and reproduce the problem you specified. Please give us a full, self-contained example that shows the problem -- don't expect us to write the data and main program. – Prune Feb 03 '20 at 22:20
  • Why are there import statements inside of a function? `random.shuffle()` only changes the order of the elements, so I'm not sure what the issue is here. If you have duplicates in your list, then you have duplicates in your list, with or without `random.shuffle()`. As an aside, you should use a context manager to handle file objects. If you just need to write a simple list to a file, there's really no need for pickling. There are multiple alternatives which will produce human readable, straightforward output. – AMC Feb 04 '20 at 00:07
  • I'm also slightly concerned by the sentence _ask users a series of questions from a list they inputted previously (pickle loads these from a previous part of my code in the example below)_. To be clear, writing the list to a file and then reading it instead of simply passing it in your program was a conscious decision? – AMC Feb 04 '20 at 00:11

1 Answers1

1

Python random.shuffle(x[, random]) reorganize the order of the list items. According to your code the repetition only possible if the saved file, from where you are loading the list through the pickle, already has repeated question.

To remove duplicates you can use set() if the type of a is list. For instance,

a = pickle.load(open("quiz.dat", "rb")))
a = list(set(a))
... 
mazhar islam
  • 5,561
  • 3
  • 20
  • 41
  • Set() is not working or my code. I get the error unhashable type: 'dict'. –  Feb 03 '20 at 23:58
  • Seems like `a` is `dict`, then there should not be any duplicate. can you print `a` and show me the output? Print after `a = pickle.load(open("quiz.dat", "rb"))) print(a)` – mazhar islam Feb 04 '20 at 00:01
  • [{'question': 'How many days in a week?', 'answer': '7'}, {'question': 'Is 2020 a leap year?', 'answer': 'yes'}, {'question': 'How many days in February', 'answer': '29'}] –  Feb 04 '20 at 00:08
  • @J_C02 You could use a list of tuples for that, or even a single dictionary. – AMC Feb 04 '20 at 00:11