Issue
So I am trying to make a sort of quiz with different categories you can choose from. It works all fine but I cant get the score function working properly. If I print(score) after three correct answers (the score should be 3) it still says 0. If I put the return score after the score += 1 it will automatically stop after one correct answer but I want to be able to give 3 correct answers.
Below is a part of the code
def quizquestion(score,question_category):
for question_number,question in enumerate(question_category):
print ("Question",question_number+1)
time.sleep(1)
slow_type (question)
for options in question_category[question][:-1]:
slow_type (options)
user_choice = input("make your choice: ")
if user_choice == question_category[question][-1]:
replay = good[random.randint(0,7)]
slow_type (replay)
score += 1
else:
replay = wrong[random.randint(0,7)]
slow_type (replay)
slow_type ("The correct answer should have been")
print(question_category[question][-1])
time.sleep(1)
slow_type("okay you finished this category, lets see what your score is in this category")
slow_type("You have"), print(score), slow_type("correct answer(s)")
return score
one of the categories:
questions_random = {
"How tall is the Eiffel Tower?":['a. 350m', 'b. 342m', 'c. 324m', 'd. 1000ft','a'],
"How loud is a sonic boom?":['a. 160dB', 'b. 175dB', 'c. 157dB', 'd. 213dB', 'd'],
"What is the highest mountain in the world?":['a. Mont Blanc', 'b. K2', 'c. Mount Everest', 'd. Mount Kilomonjaro', 'c']
}
If you need more of the code to help me please let me know
Solution
The score can be a keyword argument. That way it has a default initial value. Here is a working example derived from your code.
Note: For testing purposes, I replaced slow_type
with print
, good[random.randint(0,7)]
with "\ncorrect"
, and wrong[random.randint(0,7)]
with "\nwrong\n"
.
This works, the correct score is returned.
You can pass in the score from a previous round of questions like follows:
quizquestion(question_category, score=<current_score>)
.
You can learn more about keyword arguments here.
def quizquestion(question_category, score=0):
boundary = "*************************************************"
for question_number,question in enumerate(question_category):
print ("\nQuestion",question_number+1)
time.sleep(1)
print(question)
for options in question_category[question][:-1]:
print(options)
user_choice = input("make your choice: ")
if user_choice == question_category[question][-1]:
replay = "\ncorrect!"
print(replay)
score += 1
print(boundary)
else:
replay = "\nwrong\n"
print(replay)
print("The correct answer should have been")
print(question_category[question][-1])
print(boundary)
time.sleep(1)
print("\nokay you finished this category, lets see what your score is in this category")
print("You have {} correct answers!".format(score))
return score
Answered By - Xero Smith
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.