Python Forum

Full Version: function output
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I created a math question generator and I was wondering how do I make the function return the number of questions answered correctly?

For example (on line 6),
primary_school_quiz(0,2)
Question 1:
What is the result of 3-10? -7
Question 2:
What is the result of 10-7? 3
2
This is my code:

quiz(flag, n):
    if flag == 0:
        for i in range (1,n+1):
            n1= random.randint(0,9)
            n2= random.randint(0,9)
            x= input('Question '+str(i)+':'+'\n'+'What is the result of '+str(n1)+'-'+str(n2)+'? ')
    elif flag == 1:
        for i in range (1,n+1):
            n1= random.randint(0,9)
            n2= random.randint(0,9)
            x= input('Question '+str(i)+':'+'\n'+'What is the result of '+str(n1)+'^'+str(n2)+'? ')
        
first add code to determine if you have a correct answer, which you haven't done yet.
Then have a variable, initially set to 0,
and increment each time a correct answer is made.
display the counter wherever you wish