Back

Code Challenge: Raffle

Difficulty  

divider

Objective

Collect raffle entries until the user is finished, then draw a winner at random.

You do not know in advance how many names there will be, which is exactly why this needs a list instead of a fixed set of variables.

Skills to Practice

  • Building a list from user input
  • Looping until a sentinel value is entered
  • Choosing a random index from a list
  • Using len() to stay in bounds

Tasks

  • Create a new Python program named cc-raffle.
  • Keep asking for a name, adding each one to a list, until the user types done.
  • The word done must not end up in the list.
  • Pick a random entry from the list and announce the winner.
  • Your random index needs to land between 0 and len(names) - 1.
  • Stretch: let someone enter the same name twice for two chances, then check that it really does double their odds.

Sample Output

Sample Output
Enter a name to add to the raffle (or type 'done'): Alice [Enter]
Enter a name to add to the raffle (or type 'done'): Bob [Enter]
Enter a name to add to the raffle (or type 'done'): Charlie [Enter]
Enter a name to add to the raffle (or type 'done'): done [Enter]
Drawing a winner...
Congratulations, Bob! You are the winner of the raffle!

Commence Challenge