So the trouble I am having is the score not being properly calculated because it should have a minimum score of 0 and maximum score of 5, instead the minimum score is changed to 12, and maximum to 13. The score should be calculated if the inputted number corresponds to the answer bracket stored.
#This is an example of a list
questlist = ["Q1","t", "Q2", "f", "Q3", "t", "Q4","f", "Q5", "t"]
#I want to display each question individually, to be answered then moving on to the next question.
#breaks down the questlist into questions and answers.
newqlist = (questlist[::2])
anslist = (questlist[1::2])
#print (newqlist)
#print (anslist)
score = 0
for i in newqlist:
print (i)
answer = input("Enter 'true' for true, 'false' for false.")
if answer == ("t"):
count = 1
elif answer ==("f"):
count = 1
else:
print("invalid answer")
count = 1
for j in anslist:
if answer == j:
score =1
print (score)
#gives back the user their score once completed
# if the user gets a question correct, score will be added to a maximum of 5
percentage = ((score/5)* 100)
print ("Your score is " str(score) " out of 5 and " str(percentage) "%." )
CodePudding user response:
3 things:
- Change the loop statement to this:
for q, realAns in zip(newqlist, anslist):
This basically iterates through a list that has the question and answer paired up into tuples. So for example, the first element looks like:("Q1", "t")
- Change
if answer == ("t"):
toif answer == realAns
. This just checks whether the user's answer matches the real answer. - Under
if answer == realAns:
, change thecount = 1
toscore = 1
. Ascore
variable doesn't exist in your code, so it would just error out. - Remove the
elif answer ==("f"):
statement and its correspondingcount = 1
. - Remove the
count = 1
under your else statement. - Remove this section:
for j in anslist:
if answer == j:
score =1
Since this loop runs every time your answer a question, it'll add the number of times the answer shows up in the answer list 5 times, which probably isn't what you want if you're just counting how many answers the user got right.
Your code after these changes should look something like this:
#This is an example of a list
questlist = ["Q1","t", "Q2", "f", "Q3", "t", "Q4","f", "Q5", "t"]
#I want to display each question individually, to be answered then moving on to the next question.
#breaks down the questlist into questions and answers.
newqlist = (questlist[::2])
anslist = (questlist[1::2])
score = 0
for q, realAns in zip(newqlist, anslist):
print (q)
answer = input("Enter 'true' for true, 'false' for false.")
if answer == realAns:
score = 1
else:
print("incorrect answer")
#gives back the user their score once completed
print (score)
percentage = ((score/5)* 100)
print ("Your score is " str(score) " out of 5 and " str(percentage) "%." )