Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to make correct answer counter in python?

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: ')

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 += 1
print('You got {correct}/2 answers correct!'.format(correct=counter))

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading