[Tutor] help with random.randint (cont. -- now: pseudo code)

spir denis.spir at free.fr
Wed Feb 3 11:33:20 CET 2010

```On Wed, 03 Feb 2010 14:12:42 +0800
David <ldl08 at gmx.net> wrote:

> Hello Benno, list,
>
> thanks for those clarifications, which, well, clarify things ;-)
>
> This is my latest creation:
>
> import random
>
> def createTerms():
>      terms =  []
>      for i in range(2):
>          terms.append(random.randint(1, 99))
>      j = terms[0]
>      k = terms[1]
>      print "%3d\nx%2d" % (j, k)
>
> createTerms()

I find
j,k = terms
clearer. (This will automagically unpack items from terms.)

> Which works. However, it merely prints a multiplication task. Anyway,
> this was just a prelude. In the end, I want far more, namely to create,
> ask, and verify some multiplication questions. Here is my pseudo code of
> this little project:
>
>
> <CODE>
> pool = []
> correct = 0
> incorrect = 0
>
> def createQuestions:
>      generate all multiplication combinations possible
>      append as tuple to pool
>      eliminate 'mirrored doubles' (i.e. 7x12 and 12x7)
Unneeded. Instead building all combinations looping with j=1..n & k=1..n, directly avoid duplicates using j=1..n & k=j..n. (not 100% sure, though)

>      randomize pool
>
>      for question in pool:
>          calculate solution
>          take answer from user
>          if user answer == solution:
>              correct += 1
>              remove question from pool
>          else:
>              incorrect += 1
>
> def showStats:
>      print number of questions asked
>      print number of questions answered correctly
>      print percentage of correct answers
>
>      answer = raw_input("Try again the incorrect questions? (y/n) ")
>      if answer == "y":
>          aksQuestions()
>      else:
>          break
>
>
> createQuestions()
> showStats()
> print "good-bye!"
>
> </CODE>
>
> I think it is sensible to
>
> * first create all possible solutions, then
> * kick out doublettes, and only then
> * ask questions
>
> I have some questions though:
>
> Is the overall structure and flow of this program okay? What are the
> main problems you can spot immediately?
>
> In the very end I would like to take this code as a basis for a wxPython
> program. Are there any structural requirements I am violating here?

You should from start on organize your code into funcs that will be so-called "callback functions", meaning functions that will be called by user actions (typically button press). Hope it's clear. This does not prevent an OO structure of the code, indeed, the funcs can well be object methods if this matches the problem.

> If I want to limit the number of questions asked, say to 20, would I
> operate with slicing methods on the randomised pool?

?

> How would you go about showing stats for the second run (i.e. the
> FaultyAnswers)? Right now I am thinking of setting the global variables
> correct and incorrect to 0 from _within_ askFaultyAnswers; then I would
> run showStats() also from _within_ askFaultyAnswers. Good idea?

I would have a kind of overall "UserTest" object with methods generating and storing test data (numbers), asking questions and getting answers, recording and outputing results.
This is an OO point of view. You may organise things differently (but even then an OO pov sometimes helps structuring a pb).

Denis
________________________________

la vita e estrany

http://spir.wikidot.com/
```