Home > database >  How to make correct answer counter in python?
How to make correct answer counter in python?

Time:02-20

I have 2 questions and correct answer is 1. how to make correct answer counter and after 2 questions show user that he answered right on 2 questions if answered correct

print('what is 2 2?')

q1 = input('1)4 , 2)3 , 3)6: ')

print('what is 4-1?')

q2 = input('1)3, 2)4, 3)1: ')

CodePudding user response:

You need a variable to keep track of the number of correct answers and you need to parse the input to see if the answer was correct. Then you need to print a formatted string to display the number of answers the user got correct. So something like:

counter = 0
print('what is 2 2?')
# Ask and check first question
q1 = input('1)4 , 2)3 , 3)6: ')
if int(q1) == 1:
    counter = 1
print('what is 4-1?')
# Ask and check second question
q2 = input('1)3, 2)4, 3)1: ')
if int(q2) == 1:
    counter = 2
print('You got {correct}/2 answers correct!'.format(correct=counter))

  • Related