We need to clarify the goal. The goal is not to propose questions to
the user. the goal is to check if he answers correctly. Should each
correct answer be recorded for each user? should incorrect answers be
recorded? Should the list of proposed questions be recored? Should a
score be computed? What is the answer is "Darwin" and he answers
"darwin" or "Charles Darwin"?

Anyway, the above code can be simplified in:

db.define_table('q_and_a',
    Field('question'),
    Field('answer'))

def check(correct_answer,answer):
    if correct_answer.lower() in answer.lower(): return 1
    return 0

def ask():
    asked =
db(db.q_and_a.id>0).select(orderby='<random>',limitby=(0,3))
    form=SQLFORM.factory(*[Field('a%s'%i, label = a.question) for i,a
in enumerate(asked)])
    if form.accepts(request.vars,session):
           score = sum((check(a.answer,request.vars.get('a%s'%i,''))
for i,a in enumerate(asked))
           # do something with score
    return dict(form=form)


On Aug 22, 6:04 pm, Russell <[email protected]> wrote:
> In my experience, it is a bit awkward to do this using SQLFORM.  You
> could try something like this:
>
> db.define_table('q_and_a',
>     Field('question'),
>     Field('answer'))
>
> rows = db(db.q_and_a.id>0).select()
>
> import random
> random.seed()
> asked = random.sample(rows, 3))
>
> form=SQLFORM.factory(
>     Field('asked_0', label = asked [0]['question']),
>     Field('asked_1 ', label = asked [1]['question']),
>     Field('asked_2 ', label = asked [2]['question']))
>
> But this can create more problems than it solves.  Ultimately it might
> be easier to put your questions in a wide table and use something like
> Massimo proposed here:
>
> http://groups.google.com/group/web2py/browse_thread/thread/d0093fa190...
>
> On Aug 20, 1:10 pm, dlin <[email protected]> wrote:
>
> > Tks, Bruno.
> > I seems haven't describe the 'row base' questionaire.
>
> > That is:
>
> > I've already a table like:
> > Question Answer
> > Q1...           A1
> > Q2...           A2
> > Q3...           A3
> > ...
> > Q13...           A1
>
> > Then, I'll random choose three(or by customized) questions to ask.
>
> > Question  Answer
> > Q3.....      ____
> > Q8.....      ____
> > Q9.....      ____
> >  [submit]
>
> > sometimes, it
> > Question  Answer
> > Q7.....      ____
> > Q9.....      ____
> > Q13.....      ____
> >  [submit]
>
> > I think it is not proper to save it fixed on 3 field on table.
>
> > I'm thinking should I use following steps:
> > 1. SQLTABLE to select some random rows from my question and answer .
> > 2. try to form a FORM() (I haven't idea now)
> > 3. when user submit form, then try to compare the answer and store the
> > scores of user in another table.
>
> > On Aug 19, 7:54 am, Bruno Rocha <[email protected]> wrote:
>
> > > You can use FORM(), and also use the table configuration to define
> > > questions,
> > > you can use the arguments "label" and "comment" on field definition.
>
> > > After that, you can use CSS to improve the form layout.
>
> > > You can't create a new field to store the login info and putting by 
> > > default
> > > the auth.user value?
>
> > > 2010/8/18 dlin <[email protected]>
>
> > > > I want a form like:
>
> > > > Question  Answer
> > > > Q1.....      ____
> > > > Q2.....      ____
> > > > Q3.....      ____
> > > > [submit]
>
> > > > Q1,Q2,Q3 and correct answer is stored in a table.
> > > > But the 'Answer' field should combined with login user's answer.
>
> > > > What's the best way to display the form to user?
> > > > Do you suggest use FORM() in control or use <form> in view?
>
> > > --
>
> > >http://rochacbruno.com.br

Reply via email to