Fortune teller
Make the Great Pythonini, a fortune teller who picks your future at random.
Start here: open pygwide. On the home page, choose Tutorials, then Python Basics, then Lesson 4: Fortune teller.
This lesson takes about an hour. The extensions take about 30 minutes more.
What you will learn
- How to use code that comes with Python, with
import. - How to pick a random number with
random.randint(). - How to pick one thing from a list of choices with
random.choice().
What you need from before
Step 1: Roll a dice
Python comes with lots of extra code, kept in modules. The random
module makes random choices. To use it, you import it first:
import random
roll = random.randint(1, 6)
print(f"You rolled a {roll}.")
random.randint(1, 6) picks a whole number from 1 to 6. Run your program
a few times. Do you get a different number each time?
In Scratch
random.randint(1, 6) is like pick random 1 to 6.
Step 2: Pick from some choices
random.choice() picks one thing from a list of choices. The choices go
in square brackets [ ], with commas between them:
import random
coin = random.choice(["heads", "tails"])
print(f"The coin landed on {coin}.")
A set of things in square brackets is called a list. You'll learn lots more about lists later.
Step 3: The fortune teller
Now replace your code with the start of the fortune teller:
import random
print("I am the Great Pythonini. I can see your future!")
name = input("What is your name? ")
print(f"Let me look into my crystal ball, {name}...")
print()
Step 4: Choose a fortune
Add a list of fortunes, and pick one at random. A long list can go over several lines, as long as the brackets match:
fortune = random.choice(
[
"You will find a lost sock today.",
"A dog will smile at you.",
"You will be brilliant at Python.",
"Something yummy is coming your way.",
"You will laugh until your tummy hurts.",
]
)
print(fortune)
Change the fortunes to your own, and add some more.
Step 5: A lucky number
Last of all, give a lucky number from 1 to 100:
lucky = random.randint(1, 100)
print(f"Your lucky number is {lucky}.")
Check your code
# Fortune teller
import random
print("I am the Great Pythonini. I can see your future!")
name = input("What is your name? ")
print(f"Let me look into my crystal ball, {name}...")
print()
fortune = random.choice(
[
"You will find a lost sock today.",
"A dog will smile at you.",
"You will be brilliant at Python.",
"Something yummy is coming your way.",
"You will laugh until your tummy hurts.",
]
)
print(fortune)
lucky = random.randint(1, 100)
print(f"Your lucky number is {lucky}.")
Extensions
Roll two dice
Write a program that rolls two dice and adds them up. If both dice show the same number, it says "A double! Roll again!"
One way to do it
# Roll two dice
import random
first = random.randint(1, 6)
second = random.randint(1, 6)
print(f"You rolled a {first} and a {second}.")
print(f"That makes {first + second} altogether.")
if first == second:
print("A double! Roll again!")
A magic answer ball
Make a program that asks the player to type a question, then answers it at random with something like "Yes!", "No way!" or "Ask me again later".
Extra bits
Did you know?
Computers can't really be random. They use a clever sum that makes numbers look random, starting from something that changes all the time, such as the exact time. That's why your dice give a different number each time you run them.
Export your project
pygwide keeps your work in this web browser, but club computers can forget it. Press ⇩ Export to save your project as a zip file. Keep it somewhere safe, such as a USB stick or your school drive.
Next time, choose ⇪ Import a project on the home page to carry on with it.