Skip to content Skip to sidebar Skip to footer

How To Shuffle Questions In Python

please anyone help me i have posted my whole work down can anyone tell me how to shuffle theses five questions it please i will be very thankful. print('welcome to the quiz') Valid

Solution 1:

You can't shuffle them the way they are hard-coded. Store the questions is a list, and use the random.shuffle function on that list. You could do something like

validation = Falsewhile validation ==False:
    # input ageif age > 16:
       validation = False

    questions = [
       ('question 1 text', 'A'),
       ('question 2 text', 'C')...]

    random.shuffle(questions)

    for question_text, correct_answer in questions:
        user_input = input(question_text)

        if user_input.capitalize() == correct_answer:
           score += 5print("well done")
        else:
           print("wrong answer")

    validation = True

Solution 2:

First make a list (or a dict) with your questions and answers:

Q = [
    "What is CEOP?", #Each question and answer is defined by a number this one is 0"When you get an email from someone you do not know, what should you do?", #This one is 1"How secret should you keep your passwords?", # 2"When an online contact who frightens you asks to meet you in person what should you do?", #And so on"If an email asks you to enter your bank account details because of a problem with your account what should you do?"
    ]

A = [
    "A:Child Exploitation and Online Protection/B:Child Exploitation and Organised Protectors/C:Criminal Exploration and Online Protection",
    "A:Reply and say hello/B:Delete it and mark as spam/C:Forward to your friends",
    "A:Give them to strangers/B:Give them only to your best friends/C:Never give out passwords except to your parents",
    "A:Arrange to meet them with your best friend/B:Arrange to meet them/C:Report to CEOP",
    "A:Reply to the email/B:Contact the bank to check if they sent the email/C:Enter your bank account details",
    ]

Define a list to keep track of the shown questions:

QAsked = []

Make a definition for selecting a question:

def SearchQ():
QProposed = random.randint(0,4)
    if QProposed in QAsked: #If already asked
        SearchQ()           #Search again
    else:
        QAsked.append(QProposed)
        print(Question: Q[QProposed])
        print(Answer: A[QProposed])
        CheckIfOK(QProposed)

Remember to from random import randint.

CheckIfOK(Question) is something like:

if Question == 1:
    ans = input("Answer: ")
    if ans.capitalize() == "A":
        print("WELL DONE")
        score = score + 25elif ans.capitalize() =="B":
        print("Wrong Answer")
    elif ans.capitalize() =="C":
        print("Wrong Answer")
    else:
        print("That's not an answer")
if Question == 2:
    ans = input("Answer: ")
    if ans.capitalize() == "C":
        print("WELL DONE")
        score = score + 25elif ans.capitalize() =="B":
        print("Wrong Answer")
    elif ans.capitalize() =="A":
        print("Wrong Answer")
    else:
        print("That's not an answer")
...

Post a Comment for "How To Shuffle Questions In Python"