Python beginner here using 3.8.1 .
I am currently working on a multiple choice quiz program that works in two parts. The first part allows the users to create a series of questions+answers and save them using the Pickle module. The second part allows the users to load their save data and get quizzed by the program.
My problem is that, in the second part of the program, I want to display the multiple choice options for each question individually, but my program will end up showing the multiple choices for every single question, not just for the question currently asked by the program.
What is causing this and how can it be fixed!
Thank you for the help in advance!
#this is part one of the program, where the user creates and saves their data
def PartOne():
import pickle
import os
from time import sleep
os.system('cls')
while True:
t = input("How many questions do you have? : ")
if t.isnumeric():
break
else:
print("Please enter a valid number.")
cx = list()
ci = list()
co = list()
for i in range(0, int(t)):
c = input("Please enter the question : ")
ca = input("Please enter option A : ")
cb = input("Please enter option B : ")
cc = input("Please enter option C : ")
cd = input("Please enter option D : ")
while True:
m = input("What option coresponds to the correct answer (a, b, c or d) : ")
if m.lower() in ('a', 'b', 'c', 'd'):
cm = {"question": c, "answer": m}
cx.append(cm)
ch = {"option a": ca, "option b": cb, "option c": cc, "option d": cd}
ci.append(ch)
cu = {"question": cx, "answer": ci}
co.append(cu)
break
else:
print("Invalid answer. Please enter «a», «b», «c» or «d»")
while True:
question_cm = input("Would you like to save? (yes or no) ")
if question_cm.lower() in ('yes'):
pickle.dump(cx, open("Savedataone.dat", "wb"))
pickle.dump(ci, open("Savedatatwo.dat", "wb"))
pickle.dump(co, open("Savedatathree.dat", "wb"))
print("Saved!")
sleep(1.2)
break
if question_cm.lower() in ('no'):
print ("Please save to use this information.")
sleep(1.2)
break
else:
print("Invalid answer. Please enter yes or no.")
#This the second part of the program where the user uses what they inputed in part 1 to quiz themselves
def PartTwo():
import pickle
import random
import os
from time import sleep
os.system('cls')
while True:
person = input("How much people will participate in this study session? : ")
if person.isnumeric():
break
else:
print("Please enter a valid answer.")
for i in range(0, int(person)):
os.system('cls')
cx = pickle.load(open("Savedataone.dat", "rb"))
ci = pickle.load(open("Savedatatwo.dat", "rb"))
co = pickle.load(open("Savedatathree.dat", "rb"))
print("Study session will begin soon...")
sleep(2)
for cm in cx:
random.shuffle(cx)
print(cm["question"])
print(ci)
response = input("What was the correct answer (a, b, c or d)? : ")
if (response == cm["answer"]):
print("Good!")
continue
else:
print("Wrong...")
sleep(0.2)
print("The correct answer was", cm, ".")
continue
print("Study session is now over.")
sleep (1)
print("Next person's study session will begin soon if needed.")
sleep(1)
print("Verification...")
sleep(2.5)
sleep(1.35)
print("Study session is now over.")
Replace your current partTwo() fuction with:
def PartTwo():
import pickle
import random
import os
from time import sleep
os.system('cls')
while True:
person = input("How much people will participate in this study session? : ")
if person.isnumeric():
break
else:
print("Please enter a valid answer.")
for i in range(0, int(person)):
os.system('cls')
cx = pickle.load(open("Savedataone.dat", "rb"))
ci = pickle.load(open("Savedatatwo.dat", "rb"))
co = pickle.load(open("Savedatathree.dat", "rb"))
print("Study session will begin soon...")
sleep(2)
questionNumber=0
for cm in cx:
random.shuffle(cx)
print(cm["question"])
print(ci[questionNumber])
response = input("What was the correct answer (a, b, c or d)? : ")
if (response == cm["answer"]):
print("Good!")
questionNumber += 1
continue
else:
print("Wrong...")
sleep(0.2)
print("The correct answer was", cm, ".")
questionNumber+=1
continue
print("Study session is now over.")
sleep (1)
print("Next person's study session will begin soon if needed.")
sleep(1)
print("Verification...")
sleep(2.5)
sleep(1.35)
print("Study session is now over.")
I noticed that you appended every answer made in PartOne() in 'ci', but in partTwo() you just print ci without specifying which part, which prints everything.
I added a counter (questionNumber) that keeps track of the current question asked, that counter is used to specify which possible answers to give.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.