Python Forum
How to create a question generator?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to create a question generator?
#1
I am trying to create a function where it takes two parameters, an integer "flag" and a positive integer "n". If flag is 0, primary_school_quiz helps practice subtraction then generates n math problems that a pupil must answer in turn. For each question, it generates two random positive, single-digit numbers and asks the pupil for the answer to the math problem with those two numbers ( subtract the second number from the first) then prompts the pupil for the answer, and checks if her answer is correct. At the end of n questions, another function returns the number of questions answered correctly.

So far I have:
def primary_school_quiz(flag, n):
    if flag == 0:
        n1=(randint(0,9))
        n2=(randint(0,9))
        for i in range (n):
            q=input('Question'+str(i)+'What is the result of '+str(n1)+'-'+str(n2)+'?')
            return q 
The output is supposed to look like:

>>> 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

I don't know how to execute the problem

 if flag == 0:
        n1= random.randint(0,9)
        n2= random.randint(0,9)
        for i in range (n):
            q=input('Question'+str(i)+'What is the result of '+str(n1)+'-'+str(n2)+'? ')
            return q 
How would I make more than I question? And for the question to start at number 1?
Because the above returns:
Question0 What is the result of 8-5? 3
'3'
Reply
#2
for loop indexes start with 0, there is no way around that. But you can accomodate "str(i)" to show the value you want (incremented).

When "return" statement executes, function exits. So if you want several iterations of for loop, you will need to place return elsewhere, and also consider a meaningful information it will return after function is executed.

In "primary_school_quiz()" function you return q, which is user's input, and not (necessarily) correct answer, if that is what you intended. Also, if you want questions to be different algebra problem in each iteration, place random number generation inside for loop, so it is executed again each time. Otherwise you will get same question over and over.
Reply
#3
The loop can start from a different value than 0.

In [1]: for i in range(1, 5): # range(start, stop, step)
   ...:     print(i)
   ...:     
1
2
3
4
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
(Oct-09-2017, 05:52 AM)j.crater Wrote: for loop indexes start with 0,
er,
for i in range(1,13):
    print(i)
will print 1 .. 12.

Moderator: Post-edit content follows:
PLEASE DELETE THIS MESSAGE - basically just said same as last comment. Doesn't seem to be a delete option available to me, just an edit option.
I am trying to help you, really, even if it doesn't always seem that way
Reply
#5
(Oct-10-2017, 09:05 PM)gruntfutuk Wrote: PLEASE DELETE THIS MESSAGE - basically just said same as last comment. Doesn't seem to be a delete option available to me, just an edit option.

Sorry, we don't delete replies. They live forever plus eternity on the internets  Big Grin.  Plus there is nothing wrong with duplicate answers, happens quite often.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#6
There is a relatively new universe law which humanity is following strictly and we will prosper forever and ever. "Keep every piece of data you can get." We're learning from the best Big Grin
Not offence, Google!
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#7
(Oct-09-2017, 05:52 AM)j.crater Wrote: for loop indexes start with 0, there is no way around that. But you can accomodate "str(i)" to show the value you want (incremented).

When "return" statement executes, function exits. So if you want several iterations of for loop, you will need to place return elsewhere, and also consider a meaningful information it will return after function is executed.

In "primary_school_quiz()" function you return q, which is user's input, and not (necessarily) correct answer, if that is what you intended. Also, if you want questions to be different algebra problem in each iteration, place random number generation inside for loop, so it is executed again each time. Otherwise you will get same question over and over.

Thank you for the reply.. what would I add to the code so that it returns the number of questions answered correctly?
Reply
#8
At the line:
return q
instead of q, you would need to return a different variable, which holds number of questions answered correctly. But you first have to implement the logic that counts correct answers and stores them in that variable. Perhaps you have already done it, but the code you have originally posted doesn't have it.
Reply
#9
(Oct-12-2017, 09:06 AM)j.crater Wrote: At the line:
return q
instead of q, you would need to return a different variable, which holds number of questions answered correctly. But you first have to implement the logic that counts correct answers and stores them in that variable. Perhaps you have already done it, but the code you have originally posted doesn't have it.

How would you count correct answers when using random generator?
So far my code looks like this:
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)+'? ')
Reply
#10
A simple if statement should do it
counter = 0
if str(n1 + n2) == x:
    counter = counter + 1 # or 'counter += 1'
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020