[Python Wpg] version 2.0 for door prizes

Mike Pfaiffer high.res.mike at gmail.com
Wed Nov 28 17:24:23 EST 2007


	It's small and I had a suggestion to take care of the exception where 
there were more prizes than names. Here is the new code.

				Later
				Mike

*************************
# This program distributes door prizes for clubs

# Import modules as needed
import random

# Seed random number generator
random.seed()

# Print introductory message
print """Door prize distribution program:

This program will distribute a number of door prizes to all people who
enter their names in to this program. The program presumes no errors
in data entry.

Note: to collect the prize the person must actually be present.

The first step is to enter the prizes and the second is to enter the names
of the people participating in the draw. The third will be the actual draw.

"""

# Enter prizes
prize_count = -1
print "Enter prize list. Terminate with 'end'."
print
prize = ''
while (prize != 'end'):
      prize = raw_input('Prize --> ')
      if prize != 'end':
           prize_count = prize_count + 1
           if prize_count == 0:
                prize_list = [prize]
           else:
                prize_list = prize_list + [prize]
      else:
           print "Prize entry terminated."
           print

# List prizes
print "Prize list:"
for counter in range(prize_count + 1):
      print prize_list[counter]
print

# Enter names
name_count = -1
no_prize = 'no prize'
print "Enter names. Terminate with 'end'."
print
name = ''
while name != 'end':
      name = raw_input('Name --> ')
      if name != 'end':
           name_count = name_count + 1
           template = [name, no_prize]
           if name_count == 0:
                name_list = [template]
           else:
                name_list = name_list + [template]
      else:
           print "Name entry terminated."
           print

# List names
print "Name list:"
for counter in range(name_count + 1):
      print name_list[counter][0]
print

# More prizes than names
if name_count < prize_count:
      name = 'Redraw'
      extra_prizes = prize_count - name_count
      for counter in range(extra_prizes):
           template = [name, no_prize]
           name_list = name_list + [template]
      name_count = prize_count

# Award prizes
print "The prizes go to..."
for counter in range(prize_count + 1):
      award = int(random.random() * (name_count + 1))
      while name_list[award][1] != no_prize:
           award = int(random.random() * (name_count + 1))
      name_list[award][1] = prize_list[counter]

# Display results
for counter in range(name_count + 1):
      print name_list[counter]



More information about the Winnipeg mailing list