maths - how to generate dynamic list of answers based on equation result?

25 views
Skip to first unread message

master r2d2

unread,
Jul 28, 2020, 2:12:41 PM7/28/20
to Kivy users support
First of all, I'm quite new to python and kivy, so apologies if it's too simple question. 

I'm trying to generate question which is simple maths formula (addition, subtraction etc.), using random function, so something like this:

self.num_one = random.randint(1,10)
self.num_two = random.randint(1,10)

I would like to generate list of possible answers as a list [ ], where one of them will be correct answer and let's say 3 others will be incorrect, but based on calculations, so for example

answer = self.num_one + self.num_two
option2 = self.num_one + self.num_two + 1 

and so on. 

then, I would like those answers and options to appear (be bind) as text on the buttons. 

I managed to get functions to check if button clicked is correct answer, but really struggling to get a list of possible combinations to appear on buttons. 

I have created two separate py files, where one of them is only responsible to do all background work, calculations etc. and main file which calls functions. Not sure if that's great idea so I'm open for any suggestions. 

I appreciate any help. 

Elliot Garbus

unread,
Jul 28, 2020, 4:10:34 PM7/28/20
to kivy-...@googlegroups.com

I suggest creating a kivy ListProperty that holds your answers, a string property to hold your question.

 

Here is a start.  This displays the problems, and will create new problems.  It does not do anything with the answers.

 

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty, ListProperty
from random import randint

kv =
"""
<QuizBox>:
    orientation: 'vertical'
    Label:
        text: root.problem
        font_size: 20
    BoxLayout:
        Button:
            text: str(root.answers[0])
        Button:
            text: str(root.answers[1])
        Button:
            text: str(root.answers[2]) 
    Button:
        text: 'Next'
        on_release: root.create_problem()
QuizBox:
"""


class QuizBox(BoxLayout):
    problem = StringProperty(
'Start')
    answers = ListProperty([
0] * 3)

   
def __init__(self, **kwargs):
       
super().__init__(**kwargs)
       
self.a = 0
       
self.b = 0
       
self.create_problem()

   
def create_problem(self):
       
self.a = randint(1,10)
       
self.b = randint(1,10)
       
self.problem = f'{self.a} + {self.b} = ?'
       
self.answers[0] = self.a + self.b # add the answer...
       
for i in range(1,3):
            x = randint(
1,20)
           
while x in self.answers:  # make sure the answers are all unique
               
x = randint(1,20)
           
self.answers[i] = x
       
self.answers.sort()  # sort so the correct answer is not always in the same spot.


class QuizApp(App):
   
def build(self):
       
return Builder.load_string(kv)


QuizApp().run()

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/834d5363-d644-40e3-bca5-511310c4d2bdo%40googlegroups.com.

 

master r2d2

unread,
Aug 2, 2020, 8:06:02 AM8/2/20
to Kivy users support
Thank you Elliot, it makes sense to me now. Hopefully I can get with my project further, but this is excellent starting point ! 

To unsubscribe from this group and stop receiving emails from it, send an email to kivy-...@googlegroups.com.

Reply all
Reply to author
Forward
0 new messages