On Wed, Feb 3, 2010 at 12:21 PM, David <ld...@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 _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor