Add images dynamically based on random number
Question: I work on a simple mathematics learning programs for my 4 year old daughter
with the help of images. Based on a random number that we can call X, a for
loop will run X number of times and print an image X number of times. The
image will be selected from the list by a random number also that we can call
Y.

If X is 2 the image Y will be printed to the screen 2 times using for loop.

The problem is I do not know how to do. :P If anyone can help me with this I
would be grateful! I'm using python 3.2 and Tkinter.

Here is example code for my image list.

    
    
        self.imageListRegular = []
        self.imageListRegular.append(ImageTk.PhotoImage(file="Bilder/Bird-icon.png"))
        self.imageListRegular.append(ImageTk.PhotoImage(file="Bilder/elephant-icon.png"))      
        self.imageListRegular.append(ImageTk.PhotoImage(file="Bilder/fish-icon.png"))
        self.imageListRegular.append(ImageTk.PhotoImage(file="Bilder/ladybird-icon.png"))
        self.imageListRegular.append(ImageTk.PhotoImage(file="Bilder/zebra-icon.png"))
    

Sincerely, Robert


Answer: use the python `random` module

    
    
    import random
    image = random.choice(self.imageListRegular) #this is your 'Y' variable
    times = random.randint(1, 4) #this is your 'X' variable
    

Then do you for loop, which I imagine looks something like this (I don't have
Tkinter, so I can't test the code. This is derived from a sample
[here](http://www.wadsworth.org/spider_doc/spider/docs/python/spipylib/tkinter.html)):

    
    
    import Tkinter 
    root = Tkinter.Tk() 
    for i in xrange(times):
        Tkinter.Label(root, image=image).pack() 
    root.mainloop() # Start the GUI
    

`random.choice` returns a random element in a given sequence, so
`random.choice(["apples", "bananas", "oranges"])` would return either
"apples", "bananas", or "oranges"

`random.randint(low, high)` will return a random integer between low and high,
including low and high. So if you wanted to display the image between 1 and 4
times, `random.randint(1, 4)` would do the trick.

