0

I am new to python and would like to ask a simple question.

I am trying to get this program to loop and every time it loops, it should store a variable in a list.

Please help

This is my code so far:

import random
for x in range(1,100):
    q=random.randint(0,99)
    w=random.randint(0,99)
    e=q*w
    q=[]#This is the list
    print '%s * %s = %s'%(q,w,e)
epcisky21
  • 53
  • 1
  • 3
  • 8
  • 2
    You have a variable named `q`. You create an empty list called `q` every time you loop. You don't use the `x` in your `for` loop. To add a variable to your list, you can use `listname.append(variable)`. – Jose Magana Sep 09 '14 at 01:24

3 Answers3

3

You need to define the list outside of the loop, and append to it within the loop.

import random

# Define your list
l = []
for x in range(1,100):
    q=random.randint(0,99)
    w=random.randint(0,99)
    e=q*w
    # Append the result to your list
    l.append(e)
    print '%s * %s = %s'%(q,w,e)

print 'The list becomes: %s' % l
aknuds1
  • 65,625
  • 67
  • 195
  • 317
0

Let's go through your code line-for-line

import random

for x in range(1,100):        # this will go 1, 2, ... , 99
    q = random.randint(0, 99) # q is a random number 0-99
    w = random.randint(0, 99) # w is a random number 0-99
    e = q * w                 # e is q times w
    q = []                    # q is now an empty list
    print '%s * %s = %s' % (q,w,e)
                              # q is an empty list, so this is wrong

Obviously you're not trying to assign q to an empty list each time. You need to make a list BEFORE the loop, then keep adding to it.

some_list = []                # some_list is an empty list
for _ in range(99):           # this will go 0, 1, ... , 98
    q = random.randint(0, 99) # q is a random number 0-99
    w = random.randint(0, 99) # w is a random number 0-99
    e = q * w                 # e is q times w
    some_list.append(e)       # add e to the list!
    print "%s * %s = %s" % (q,w,e)
                              # print "q * w = e"

Note that when you get a bit more advanced, you can wrap this up nicely as a list comprehension so you don't have to initialize it empty then add to it.

some_list = [random.randint(0,99) * random.randint(0,99) for _ in range(99)]
# equivalent to your code

But this is still a bit past your comfort level. One step at a time! :)

Things to note here that I changed:

  • range is a half-open range, e.g. range(1,100) is 99 numbers, 1-99. It does not include 100.
  • You don't actually USE any of the numbers in your range, you just want to run your loop that many times. The common idiom for this is for _ in range(some_number). You use an underscore (_) to represent to other coders that you don't use the variable here (note that _ does not represent anything special to Python, it's just convention)
  • for _ in range(N) is a quick way to do something N times.
  • list.append adds to a list =)
Adam Smith
  • 52,157
  • 12
  • 73
  • 112
0

A few notes on style:

  1. don't define a list inside a for loop, q=[] in this case, if you want it to store values acrovss the several iterations of the for loop.
  2. don't use the same name to define multiple objects inside a loop, q in this case being used both for storing a random number and as a list.

Ways to adding to list:

  1. Use append() function of the list object to append a value to the list
  2. Overload + operator to concatenate a new value to the original list

So, here's how i would change your code:

  1. First define the list outside the loop:

    import random
    e = []
    
  2. Inside the for loop, use append() function to add value to the master list, e:

    for x in range(0,100):
        q=random.randint(0,99)
        w=random.randint(0,99)
        e_val=q*w    # new local variable that resets at each iteration of the loop
        e.append(e_val)
        print '%s * %s = %s'%(q,w,e_val)
    
  3. Alternatively, you could have replaced the line e.append(e_val) with the following:

    e += [e_val]
    
Pacific Stickler
  • 1,078
  • 8
  • 20