make a random quiz with list in python

Question:

    def ask_questions():
        choice = (random.choice(question))    
        print(choice)
        if choice == 0:
            print options[0]
            answer0 = raw_input(inputs)
        if answer0 == answers[0]:
                print("correct")
        else:
            print("incorrect")
        elif choice == 1:
            print choice
            print options[1]
            answer1 = raw_input(inputs)
        if answer1 == answers[1]:
            print("correct")
        else:
            print("incorrect")
        elif choice == 2:
            print choice
            print options[2] 
            answer2 = raw_input(inputs)
        if answer2 == answers[2]:
            print("correct")
        else:
            print("incorrect")
        elif choice == 3:
            print choice
            print options[3]
            answer3 = raw_input(inputs)
        if answer3 == answers[3]:
            print("correct")
        else:
            print("incorrect")
        elif choice == 4:
            print choice
            print options[4]
            answer4 = raw_input(inputs)
        if answer4 == answers[4]:
            print("correct")
        else:
            print("incorrect")
        elif choice == 5:
            print choice
            print options[5]
            answer5 = raw_input(inputs)
        if answer5 == answers[5]:
            print("correct")
        else:
            print("incorrect")
 def main()
    date()
    quiz_infos()
    welcome()
    ask_questions()
main()

I Would like to make a random chose of questions from list

i would like to know a way to make a random choice of the questions from the list and if that question is 1: to print the option 1 and my raw_input(inputs) same goes for question 2 3 4 etc
idk why my code dose not actually do that and prints just the question, so if elif functions dose not work!
im new to python(new in coding) so i might be doing something wrong for sure,
ny the way by variable[[[[ inputs = “What do you think the answer is?”]]]]
Thanks in regard!
the code is written in python 2.7 idle

Asked By: Altin

||

Answers:

I personally would make each question:answer in a dictionary

Questions = {"What is your favorite Color?":"Blue","How many cats do I own?": "2"}

then you could return a random selection from the list using the KEYS method

import random
Questions = {"What is your favorite Color?":"Blue","How many cats do I own?": "2"}
random.choice(Questions.keys())

You can look here for dictionary information: Dictionary
and also here for the random module Random

Answered By: Taku_

There are simpler ways to do this, but here is your exact code modified for python 3 syntax with appropriate indentation (that works if the lists “question”, “options” and “answers” as well as the constant “inputs” are all actually defined and not empty).

def ask_questions():
    choice = (random.choice(question))    
    if choice == 0:
        print(choice)
        print(options[0])
        answer0 = input(inputs)
        if answer0 == answers[0]:
                print("correct")
        else:
            print("incorrect")
    elif choice == 1:
        print(choice)
        print(options[1])
        answer1 = input(inputs)
        if answer1 == answers[1]:
            print("correct")
        else:
            print("incorrect")

etc…

C:UsersmeDocuments>python test.py
0
Question 1
User Input: Answer 1
correct
C:UsersmeDocuments>python test.py
4
Question 5
User Input: Answer 5
correct

As I said above, you have indentation errors in what you pasted in your submission if not also in your code. If your code hangs after printing the question, I suspect your “inputs” may be an empty string.

Answered By: Aaron Lael

Avoid adding a block of code for each question & answer by iterating the conditional statement through a single randomized list of lists.

Randomize the list of lists using .shuffle() method. The order inside the nested lists won’t change, so for example L[0][0] is the question and L[0][1] is the answer of the first element of the randomized list.

Make the input case-insensitive with .lower() method.

Optional: add a count of right and wrong answers.

import random

L = [["Who is Batman's sidekick ?: ", "Robin"], ["Which is the capital of North Ireland ?: ", "Belfast"], ["What do caterpillars turn into ?: ", "Butterflies"]]
random.shuffle(L)
r = 0 # right answers count
w = 0 # wrong answers count
for i in range(0, len(L), 1):
    user_input = input(L[i][0])
    if user_input.lower() == L[i][1].lower():
        print("That's right !")
        r = r + 1
    else:
        print("Wrong answer")
        w = w + 1
print(str(r) + " right and " + str(w) + " wrong answers")
Answered By: Adrien Esquerre
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.