Code: Select all
import random
from string import ascii_lowercase
def get_ext(file_type):
SAMPLE = {
'Who is the founder of Microsoft?': ['Bill Gates', 'Bill Clinton', 'Jeff Bezos', 'Elon Musk'],
'Who is the founder of Amazon?': ['Jeff Bezos', 'Bill Gates', 'Elon Musk', 'Joe Biden'],
'Who created Linux?': ['Linus Torvalds', 'Elon Musk', 'Bill Gates', 'Mark Zuckerberg']
}
return locals()[file_type]
def startQuiz():
try:
questionnaire = get_ext("SAMPLE")
tItems = (len(questionnaire))
except:
print ("Something is wrong")
questionPerStudent = min(3, len(questionnaire))
questionnaire = random.sample(list(questionnaire.items()), k=questionPerStudent)
num_correct = 0
currentQ = 0
for num, (questionnaire, alternatives) in enumerate(questionnaire, start=1):
print ("{}. {}".format(num,questionnaire))
currentQ += 1
correct_answer = alternatives[0]
labeled_alternatives = dict(zip(ascii_lowercase, random.sample(alternatives, k=len(alternatives))))
key_list = list(labeled_alternatives.keys())
val_list = list(labeled_alternatives.values())
position = val_list.index(str(correct_answer))
keychoice = key_list[position]
for label, alternative in labeled_alternatives.items():
print(f" {label}) {alternative}")
while (answer_label := input(" Your answer is? ")) not in labeled_alternatives:
print(f" Please answer one of {', '.join(labeled_alternatives)}")
answer = labeled_alternatives[answer_label]
if answer == correct_answer:
num_correct += 1
print(f"Correct : {keychoice}) {correct_answer}")
else:
print(f"Wrong Answer : {answer_label}) {answer}")
print(f"Correct Answer: {keychoice}) {correct_answer}")
startQuiz()
Code: Select all
1. Who is the founder of Microsoft?
a) Jeff Bezos
b) Bill Gates
c) Elon Musk
d) Bill Clinton
Your answer is? a
Wrong Answer : a) Jeff Bezos
Correct Answer: b) Bill Gates
2. Who is the founder of Amazon?
a) Joe Biden
b) Elon Musk
c) Bill Gates
d) Jeff Bezos
Your answer is? a
Wrong Answer : a) Joe Biden
Correct Answer: d) Jeff Bezos
Code: Select all
'Who is the founder of Microsoft?': ['Bill Gates', 'Bill Clinton', 'Jeff Bezos', 'Elon Musk'] info = "this is the explaination",
Code: Select all
1. Who is the founder of Microsoft?
a) Jeff Bezos
b) Bill Gates
c) Elon Musk
d) Bill Clinton
Your answer is? a
Wrong Answer : a) Jeff Bezos
Correct Answer: b) Bill Gates
? this is the explanation
Mobile version