[Tutor] help with random.randint
David
ldl08 at gmx.net
Wed Feb 3 04:21:56 CET 2010
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)
print terms
This prints just one number (the last one generated in the loop?)
So I tried to change line 4 to the following:
terms += random.randint(1, 99)
hoping that it would add a second integer to my terms list. But I get an
error:
/home/david/Documents/Python-Projekt/multiplier.py in <module>()
2 terms = []
3 for i in range(2):
----> 4 terms += random.randint(1, 99)
5 print terms
6
TypeError: 'int' object is not iterable
WARNING: Failure executing file: <multiplier.py>
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!
So here are my questions: what is the problem, and how can I generate
two random numbers and store them (preferably in a tuple)?
Many thanks,
David
More information about the Tutor
mailing list