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

David ldl08 at gmx.net
Wed Feb 3 07:12:42 CET 2010

```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()

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)
randomize pool

for question in pool:
calculate solution
correct += 1
remove question from pool
else:
incorrect += 1

def showStats:
print number of questions answered correctly

answer = raw_input("Try again the incorrect questions? (y/n) ")
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

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?

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

Many thanks for your guidance and input!

David

On 03/02/10 12:25, Benno Lang wrote:
> On Wed, Feb 3, 2010 at 12:21 PM, David<ldl08 at gmx.net>  wrote:
>> Hello list,
>>
>> I thought this was easy even for me, but I was wrong, I guess.
>> Here is what I want to do: take two random numbers between 1 and 99, and put
>> them into a list.
>>
>> import random
>> terms =  []
>> for i in range(2):
>>         terms = random.randint(1, 99)
>
> All you're doing here is assigning an integer value to 'terms', twice.
> This assignment means 'terms' is no longer a list, but is now just an
> int. What you want is probably:
> terms.append (random.randint(1, 99))
>
>> So I tried to change line 4 to the following:
>>         terms += random.randint(1, 99)
>
> You can't freely mix types with python operators, i.e. a_list += an_int
> But you can use += with 2 ints or 2 lists, so you could do:
> terms += [random.randint(1, 99)]
> I think using append is much nicer though.
>
>> I understand this error thus: once an int has been placed into the list
>> terms, no further int can be added. But: terms is a mutable list, and NOT an
>> 'int' object!
>
> The int was never added to the list in the first place, because list
> += int is not something Python understands.
>
>> So here are my questions: what is the problem, and how can I generate two
>> random numbers and store them (preferably in a tuple)?
>
> I hope what I wrote above answers the first question.
> IIRC tuples are immutable, so you either to create the list first and
> then convert it to a tuple:
> terms_tuple = tuple(terms)
>
> Or you can create a tuple from the beginning (without a loop):
> terms_tuple = (random.randint(1, 99), random.randint(1, 99))
>
> HTH,
> benno
>

```