Slot Machine
A slot machine is just a random pick from a set of symbols, three times in a row.
You already know random; now you'll store the symbols in a list and pull
from it.
Overview
You'll build a Python list of symbols and write a slot function that prints
three random symbols from it β a one-line jackpot generator.
You've got it whenβ¦
- You have a list holding each of the slot symbols.
-
random.choice()pulls a random symbol from the list. - Your
slotfunction prints three random symbols.
Collaboration & AI
Work: On your own. A neighbor can help, but write your own function.
AI β AIAS Level 1, No AI: Build the list and loop yourself. What the levels mean.
Create a List of Strings
Create a Python list of strings, with one string for each of these symbols:
Tip
Use the copy button on the block above to grab the symbols, then split them into separate strings in your list.
Random Selection
We used the random library
before to pick random numbers with randint. Now we'll use another function from
random named choice to pick a random item from a list.
Put this at the top of your file:
Use it like this, where mylist is the name of your list variable:
Try writing code to print a random item from the list in the section above.
A Slot Machine
Write a function named slot:
slotaccepts a single argument: a list of symbol strings to choose from.- Use a
forloop withrange()to repeat this three times:- Make a random selection from the list.
printthe selected symbol to the screen.
Write a main program that creates the list from the first section, then passes it
to your slot function.
Turn It In
- Your Python (
.py) file.
How It's Graded
This lab is worth up to 4 points. One score covers everything you turn in.
| Score | What it looks like |
|---|---|
| 4 β Excellent | Your list holds all eight symbols, slot takes the list as its argument and uses a for loop with range() to print three random.choice picks, and the main program builds the list and passes it in β the function works from what it's given, not from a global it happens to see. |
| 3 β Above Average | The machine spins correctly, with a minor slip β a symbol or two missing from the list, or an extra print cluttering the output. |
| 2 β Average | Three random symbols appear, but by copy-paste instead of a loop, or without a slot function taking the list. |
| 1 β Below Average | The list exists but little else runs. |
| 0 β Failing | Nothing submitted, or no evidence of the machine. |