Silly stories
Make a program that asks you questions, then uses your answers in a silly story.
Start here: open pygwide. On the home page, choose Tutorials, then Python Basics, then Lesson 2: Silly stories.
This lesson takes about an hour. The extensions take about 30 minutes more.
What you will learn
- How to keep things in variables.
- How to ask a question with
input(). - How to put variables into a message with an f-string.
- How to turn an answer into a number with
int(), and do sums with it.
What you need from before
- Showing messages with
print(), from Lesson 1. - Strings go inside quote marks, from Lesson 1.
Step 1: Variables
A variable is a name for something you want Python to remember. Type this and run it:
pet = "dog"
print(pet)
The = means "remember this as". So pet = "dog" means "remember "dog"
as pet". When you write pet without quote marks, Python looks up what
pet holds.
Now change "dog" to "hamster" and run it again.
In Scratch
pet = "dog" is like set pet to dog, using a variable called
pet.
Step 2: Ask a question
input() asks a question and waits for an answer. Replace your code with
this:
name = input("What is your name? ")
print("Hello")
print(name)
Run it. Type your name in the console, then press Enter. Python keeps
your answer in the variable name.
The space after the ? stops your answer touching the question.
In Scratch
input() is like ask and wait. The variable keeps the answer, like
Scratch's answer block.
Step 3: Put answers into messages
An f-string lets you put a variable inside a message. Put an f just
before the first quote mark, and the variable's name in curly brackets
{ }:
name = input("What is your name? ")
print(f"Hello, {name}! Nice to meet you.")
Run it. Python swaps {name} for your answer.
Step 4: Numbers
input() always gives you a string, even if you type a number. Try this:
number = input("Pick a number: ")
print(number * 2)
If you type 3, it prints 33, not 6! That's because "3" is text,
and doubling text writes it twice.
To turn the answer into a number you can do sums with, use int():
number = int(input("Pick a number: "))
print(number * 2)
Now 3 gives 6. In Python, + adds, - takes away, * multiplies and
/ divides.
Step 5: Write the silly story
Now put it all together. Replace your code with your story:
print("Answer these questions to make a silly story.")
name = input("What is your name? ")
animal = input("Name an animal: ")
food = input("Name a food: ")
place = input("Name a place: ")
number = int(input("Pick a number: "))
print()
print(f"One day, {name} went to {place}.")
print(f"There they met {number} hungry {animal}s.")
print(f"The {animal}s wanted to eat {food}.")
print(f"So {name} made {number * 2} plates of {food} for them.")
print("Everyone lived happily ever after. The end!")
Run it a few times with different answers. Then change the story to make it your own.
Check your code
# Silly story generator
print("Answer these questions to make a silly story.")
name = input("What is your name? ")
animal = input("Name an animal: ")
food = input("Name a food: ")
place = input("Name a place: ")
number = int(input("Pick a number: "))
print()
print(f"One day, {name} went to {place}.")
print(f"There they met {number} hungry {animal}s.")
print(f"The {animal}s wanted to eat {food}.")
print(f"So {name} made {number * 2} plates of {food} for them.")
print("Everyone lived happily ever after. The end!")
Extensions
How many days old are you?
Write a new program that asks for your name and your age, then says about how many days old you are. A year has 365 days. Can you also work out how many hours that is?
One way to do it
# How many days old are you?
name = input("What is your name? ")
age = int(input("How old are you? "))
days = age * 365
hours = days * 24
print(f"{name}, you are about {days} days old!")
print(f"That is about {hours} hours!")
A longer story
Ask more questions, and make your story longer. Can you make a story that uses the number in a sum, such as "there were 3 cakes, and each cake had 12 cherries, so that's 36 cherries"?
Extra bits
Fix the bugs
Each of these programs has one mistake. Can you fix them?
my name = input("What is your name? ")
print(f"Hello, {my name}!")
age = input("How old are you? ")
print(age + 1)
The first has a space in a variable's name, which Python does not allow.
Use my_name instead. In the second, age is a string. What do you need
to add?
Did you know?
A variable's name can use letters, numbers and _, but it can't start with
a number. Good names say what the variable holds, such as score or
favourite_food.
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.