I'm trying to make a multiple choice quiz using python. It seemed like something simple to make at first but now i'm scratching my head a lot trying to figure out how to do certain things.
I am using two lists; one for the questions and one for the answers. I would like to make it so that a random question is chosen from the questions list, as well as 2 random items from the answers list (wrong answers) and finally the correct answer (which has the same index as
the randomly selected question). I've got as far as selecting a random question and two random wrong answers
My first problem is getting the program to display the correct answer.
The second problem is how to check whether the user enters the correct answer or not.
I would appreciate any feedback for my code. I'm very new to this so go a little easy please! I hope my stuff is readable (sorry it's a bit long)
thanks in advance
import random
print ("Welcome to your giongo quiz\n")
x=0
while True:
def begin(): # would you like to begin? yes/no... leaves if no
wanna_begin = input("Would you like to begin?: ").lower()
if wanna_begin == ("yes"):
print ("Ok let's go!\n")
get_username()#gets the user name
elif wanna_begin != ("no") and wanna_begin != ("yes"):
print ("I don't understand, please enter yes or no:")
return begin()
elif wanna_begin == ("no"):
print ("Ok seeya!")
break
def get_username(): #get's username, checks whether it's the right length, says hello
username = input("Please choose a username (1-10 caracters):")
if len(username)>10 or len(username)<1:
print("Username too long or too short, try again")
return get_username()
else:
print ("Hello", username, ", let's get started\n\n")
return randomq(questions)
##########
questions = ["waku waku", "goro goro", "kushu", "bukubuku", "wai-wai", "poro", "kipashi", "juru", "pero"]
answers = ["excited", "cat purring", "sneezing", "bubbling", "children playing", "teardrops falling", "crunch", "slurp", "licking"]
score = 0
if len(questions) > 0: #I put this here because if i left it in ONly inside the function, it didnt work...
random_item = random.randint(0, len(questions)-1)
asked_question = questions.pop(random_item)
###########
def randomq(questions):
if len(questions) > 0:
random_item = random.randint(0, len(questions)-1)
asked_question = questions.pop(random_item)
print ("what does this onomatopea correspond to?\n\n%s" % asked_question, ":\n" )
return choices(answers, random_item)
else:
print("You have answered all the questions")
#return final_score
def choices(answers, random_item):
random_item = random.randint(0, len(questions)-1)
a1 = answers.pop(random_item)
a2 = answers.pop(random_item)
possible_ans = [a1, a2, asked_question"""must find way for this to be right answer"""]
random.shuffle(possible_ans)
n = 1
for i in possible_ans:
print (n, "-", i)
n +=1
choice = input("Enter your choice :")
"""return check_answer(asked_question, choice)"""
a = questions.index(asked_question)
b = answers.index(choice)
if a == b:
return True
return False
begin()
This is a script to make you do fast multiple choice questions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from random import shuffle
alt1="""red, rosso
yellow, giallo
green, verde
white, bianco"""
alt1=alt1.splitlines()
qnum=len(alt1)
sol=alt1.copy()
questions=[]
answers=[]
forqinalt1:
q,a=q.split(",")
questions.append(q)
answers.append(a)
qna=[]
lettsol=[]
letters="abcd"
forninrange(qnum):
a1=sol[n].split(",")[1]
pos=answers.index(a1)
answers.pop(pos)
a2,a3,a4=answers
qq=f"What is the italian word for {questions[n]}"
x=[a1,a2,a3,a4]
shuffle(x)
right=x.index(a1)
lettsol.append(letters[right])
qna.append([qq,x])
answers.insert(pos,a1)
# print(*qna, sep="\n")
counter=0
forqinqna:
q,a=q
print(q)
forans ina:
print(f"{letters[counter]})"+ans)
counter+=1
counter=0
print()
print("\nSolutions")
counter=0
forss insol:
q,s=ss.split(",")
print(q,s," ["+lettsol[counter]+"]")
counter+=1
This is the output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
What isthe italian wordforred
a)verde
b)bianco
c)giallo
d)rosso
What isthe italian wordforyellow
a)giallo
b)rosso
c)verde
d)bianco
What isthe italian
wordforgreen
a)bianco
b)rosso
c)verde
d)giallo
What isthe italian wordforwhite
a)bianco
b)rosso
c)verde
d)giallo
Solutions
red rosso[d]
yellow giallo[a]
green verde[c]
white bianco[a]
>>>
Putting more than 4 questions but having always 4 multiple choices
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from random import shuffle,sample
alt1="""red, rosso
yellow, giallo
green, verde
white, bianco
black, nero
orange, arancione"""
alt1=alt1.splitlines()
qnum=len(alt1)
sol=alt1.copy()
questions=[]
answers=[]
forqinalt1:
q,a=q.split(",")
questions.append(q)
answers.append(a)
qna=[]
lettsol=[]
letters="abcd"
forninrange(qnum):
a1=sol[n].split(",")[1]
pos=answers.index(a1)
answers.pop(pos)
shuffle(answers)
a2,a3,a4=sample(answers,3)
qq=f"What is the italian word for {questions[n]}"
x=[a1,a2,a3,a4]
shuffle(x)
right=x.index(a1)
lettsol.append(letters[right])
qna.append([qq,x])
answers.insert(pos,a1)
# print(*qna, sep="\n")
counter=0
forqinqna:
q,a=q
print(q)
forans ina:
print(f"{letters[counter]})"+ans)
counter+=1
counter=0
print()
print("\nSolutions")
counter=0
forss insol:
q,s=ss.split(",")
print(q,s," ["+lettsol[counter]+"]")
counter+=1
output
Subscribe to the newsletter for updates Post written
by
A python enthusiast
My youtube channel Twitter:
@pythonprogrammi - python_pygame
Higlighted videos
Speech recognition game Pygame cheatsheets Videos about this map editor New free game: Crystal of time
How to make a map editor 1 How to make a map editor 2 How to make a map editor 3 Map editor 1.5 Map editor 1.6 How to make a
videogame map editor with Python - MEP v. 2.1 Map editor for 2d platform game in Python with Pygame v.3.0 How to save a list with pickle with python Pygame Map Editor 4.0 for Crystals of time Github repository Newest
branch (to clone it with git) git clone --branch cotb2 https://github.com/formazione/timecrystals.git
Related
Continue Reading
How do you randomize a quiz question in Python?
how to make a random question generator in python.
num1 = random. randint(1, 10) .
num2 = random. randint(1, 10) .
answer = int(input(f"What is {num1} + {num2}?\n")) .
if answer == (num1 + num2): print("Correct").
How do I create a quiz in Python?
Build a Quiz Application With Python.
Demo: Your Python Quiz Application..
Project Overview..
Prerequisites..
Step 1: Ask Questions. ... .
Step 2: Make Your Application User-Friendly. ... .