1.2 - Lucky draw
You have two parallel arrays: one stores participant names, the other stores ticket numbers. First, display the full list of participants with their ticket numbers. Then use random to pick a winning index and display both the name and the ticket number of the winner.
names = ["Sam", "Alex", "Jordan", "Taylor"] tickets = [42, 15, 87, 63]
Participants: Sam - 42 Alex - 15 Jordan - 87 Taylor - 63 Winner: Jordan with ticket number 87
names = ["Sam", "Alex", "Jordan", "Taylor"]
tickets = [42, 15, 87, 63]
print "Participants:"
loop for length of names array
print name and ticket number at current position
print blank line
pick a random number between 0 and length of names array - 1 and store as winner_index
print "Winner:", name and tickets at winner_index
import random
names = ["Sam", "Alex", "Jordan", "Taylor"]
tickets = [42, 15, 87, 63]
print("Participants:")
for i in range(len(names)):
print(names[i] + " - " + str(tickets[i]))
print()
winner_index = random.randint(0, len(names) - 1)
print("Winner:", names[winner_index], "with ticket number" , str(tickets[winner_index]))
import random # Import the random module to pick a winner
# Parallel arrays: one stores participant names, the other stores their ticket numbers
names = ["Sam", "Alex", "Jordan", "Taylor"]
tickets = [42, 15, 87, 63]
# Display all participants with their ticket numbers
print("Participants:")
for i in range(len(names)):
print(names[i] + " - " + str(tickets[i]))
print() # Print a blank line for spacing
# Pick a random index between 0 and the last position in the array
winner_index = random.randint(0, len(names) - 1)
# Display the winner's name and matching ticket number
print("Winner:", names[winner_index], "with ticket number", str(tickets[winner_index]))
Extension
Pick a first-place winner at random, then pick a second-place winner. Keep picking the second-place winner until it is not the same person as the first-place winner. Display both winners.
Participants: Sam - 42 Alex - 15 Jordan - 87 Taylor - 63 First place: Sam with ticket number 42 Second place: Taylor with ticket number 63