25/November - Project Overview & Adventures in Python
Prerequisites
- [Optional]: Finish the materials from the previous lesson
Class Curriculum
Section content | Expected time (mins) | Pre - Requirements |
---|---|---|
Check-in on questions from last class | 5 minutes | ❌ |
Lesson Goals | 5 minutes | ❌ |
Final Project Overview & Brainstorming | 20-60 minutes | ❌ |
Break | 10 minutes | ❌ |
Kahoot | 10 - 15 minutes | ❌ |
Start working through Python activity | rest of class | ❌ |
Check-In
Respond to the post on the class slack channel responding to the following question:
- What was particularly challenging last class? Are there any remaining questions from last class?
Lesson Goals
-
Get an overview of the final project phase, generate ideas for this, and brainstorm + submit ideas for project(s) that you would be interested in working on
-
Build a text adventure game in Python and practice:
- Object-oriented programming concepts
- Flow control
- Methods and functions
Final Project Overview
Optional: please watch this video from the ReDI school explaining what the final project is.
In the next sessions, you will be working in small groups (2-3 people) on a project. In today's class, we will brainstorm a list of possible projects. Later, we will ask you to rank your preferences, and the teachers will then put you in project groups. At the end of the semester, you will have the chance to present your project at the ReDI Demo Day.
This project is a chance to have fun and gain more practice with some topic(s) that you have seen this semester. If you are particularly interested in continuing with Python or web development, this is a great opportunity to work on a project relating to one of these areas, so that you are even more prepared for the coming semesters!
Here are some example projects from last semester, to give you a sense of what is possible:
- "Lease contract app for apartments": A command line app written in Python that creates an apartment contract based on user inputs (i.e. takes in a user's name, address, etc. and outputs the filled-in contract)
- "Plant watering app": An app written in Python that reminds you when to water your plants and put them into the sun based on your location
- "Geography Quiz Game": A command line geography quiz game in python that prompts users with multiple choice quiz questions, asks the user to choose the correct answer, and gives the user a total score
- "Event website": Event finder website written in HTML/CSS and Javascript
- "TODO List": TODO List user interface (UI) written in HTML/CSS and Javascript
Other possible project ideas:
- Game of some kind (Python)
- Website (HTML/CSS, JS)
- Scavenger hunt Github Repo (Command line, git, Github)
- Extended version of the Deck of Cards/Card Game from the object-oriented Python activity (Python)
- Mini web-scraper (Python)
- ???
Kahoot: Python Classes Review
Let's do a Kahoot to check our understanding of the concepts we've learned so far.
Python Activity
0. Introduction
We are going to use Python to create a text adventure which runs on the command line.
The story
will follow you as the main character
, and your attempt to travel out of
Berlin to visit Müggelsee on a sunny day.
You will have to choose a mode of transport
and will encounter different obstacles
on your way to the lake. The modes of transport that you can choose are walking,
bicycle or S-Bahn.
Based on the choices you make, the game will have a different outcome.
DEMO
1. Setup project
- Create a new folder called
python_adventures
- In the folder, create a new file called,
adventure.py
2. Classes and Objects
Brainstorm what you would want to represent as objects in order to create a story which contains a character and different modes of transport.
- What classes can you come up with?
- What instance attributes could they have?
- What class methods could they have?
SOLUTION
There are many ways that you could solve this, which means there is no exact right or wrong answer. However, for the purpose of this exercise, we will create the following:
- A
Story
class, with three methods:start
,middle
,end
. - A
Character
class, with two instance attributes:name
andtransportation
. - A
Transportation
class, with two instance attributes:speed
anddescription
, and with one method:encounter_obstacles
- Since we have three methods of transportation, we will have three subclasses of transportation:
Walk
,Bicycle
andSBahn
.
i. Go ahead and create these classes and methods in adventure.py
.
For now, leave the methods empty.
SOLUTION - Story Class
1class Story:
2 def __init__(self):
3 pass
4
5 def start(self):
6 pass
7
8 def middle(self, character, transportation):
9 pass
10
11 def end(self, character):
12 pass
13
14 new_story = Story()
15 new_story.start()
SOLUTION - Character Class
1class Character:
2 def __init__(self, name, transportation):
3 self.name = name
4 self.transportation = transportation
When defining the Bicycle
, Walk
and SBahn
classes, you can already set the following name
, speed
, and description
:
Walk
name: "walking"
speed: "slow"
description: "It is a long and tiring walk, but scenic.🚶"
Bicycle
name: "bicycle"
speed: "average"
description: "Efficient and green - cycling is a great way to explore! 🚲"
S-Bahn
name: "S-Bahn"
speed: "fast"
description: "The S-Bahn is busy with people getting out of the city today, but you can read a book, listen to music and gaze out the window. 🚉"
SOLUTION - Transportation Classes
1class Transportation:
2 def __init__(self, name, speed, description, obstacles):
3 self.name = name
4 self.speed = speed
5 self.description = description
6
7 def encounter_obstacles(self):
8 pass
9
10
11class Walk(Transportation):
12 def __init__(self):
13 self.name = "walking"
14 self.speed = "slow"
15 self.description = "It is a long and tiring walk, but scenic.🚶"
16
17 def encounter_obstacles(self):
18 pass
19
20
21class SBahn(Transportation):
22 def __init__(self):
23 self.name = "sbahn"
24 self.speed = "fast"
25 self.description = "The S-Bahn is busy with people getting out of the city today, but you can read a book, listen to music and gaze out the window. 🚉"
26
27 def encounter_obstacles(self):
28 pass
29
30
31class Bicycle(Transportation):
32 def __init__(self):
33 self.name = "bicycle"
34 self.speed = "average"
35 self.description = "Efficient and green - cycling is a great way to explore! 🚲"
36
37 def encounter_obstacles(self):
38 pass
ii. At the end of the file, create a new_story
object from the Story
class. Call
the start
method on new story, so that a new story is created when the program is run.
3. Create the story introduction
The introduction of the story will always be the same. We should ask the player for their name, introduce the story with some opening text and ask the player which mode of transport they would like to take.
i. First, we want to create an introduction for the player to start the story. Please copy and paste the following function into python_adventure.py
. This function should be kept outside of any class. (Ordinarily you should not copy and paste from tutorials, but in this case there is a lot of text which is not useful for you to type out):
1def introduction_text(name):
2 print("")
3 print(
4 "It's a beautiful summer's day in Berlin, and you are stuck in Hermannplatz :("
5 )
6 print(
7 "You don't want to be here. It is too noisy, there is too much traffic and all this concrete makes it uncomfortably warm"
8 )
9 print("The lakes are calling...")
10 print("") # The purpose of this print statement is to make the CLI output easier to read
11 print(f"Well {name}, today we go to Müggelsee!")
12 print("")
ii. Go to the start()
method of the Story
class. Using the input()
method, ask the player what their name is. Assign their input to a variable, name
.
iii. In the next line of start()
, call the function introduction_text
and pass in the name
variable as an argument.
iv. In the next line of start()
, ask the user how they plan to travel to the lake using the string "The only question is, how should we go to the lake? a. walking, b. bicycle or c. sbahn\n"
. Assign the input to a variable called choice
.
v. In the next part of start()
, we will be using an if, elif, else
statement to check the choice
. If it is a
, create a new variable called transportation
and assign the object Walk()
to it, if choice is b
assign the object Bicycle()
to transportation
, otherwise if the choice is c
assign the object SBahn()
to transportation
. Use the else
branch to catch the scenario where a player inputs none of these options by printing a message to the player and exiting
.
vi. In the next part of start, we want to create a Character
object, using the name
and transportation
choice that the player has given us already.
vii. Finally, to complete this method, we will want to call the method to start
the next part of the story middle
, and pass the character
and transportation
objects to it.
SOLUTION - START
1def introduction_text(name):
2 print("")
3 print(
4 "It's a beautiful summer's day in Berlin, and you are stuck in Hermannplatz :("
5 )
6 print(
7 "You don't want to be here. It is too noisy, there is too much traffic and all this concrete makes it uncomfortably warm"
8 )
9 print("The lakes are calling...")
10 print("")
11 print(f"Well {name}, today we go to Müggelsee!")
12 print("")
13
14class Story:
15 def __init__(self):
16 pass
17
18 def start(self):
19 name = input("What is your name?\n")
20 introduction_text(name)
21
22 choice = input(
23 "The only question is, how should we go to the lake? a. walking, b. bicycle or c. sbahn\n"
24 )
25 if choice == "a":
26 transportation = Walk()
27 elif choice == "b":
28 transportation = Bicycle()
29 elif choice == "c":
30 transportation = SBahn()
31 else:
32 print("You have not selected a valid option!")
33 exit()
34
35 character = Character(name, transportation)
36
37 self.middle(character, transportation)
Run the program and see what happens
4. The middle of the story
The middle or content of the story will vary depending on the mode of transportation
that the user takes. We will use the middle()
method as a way to begin this in the same
way for each mode of transport, and then use the encounter_obstacles()
method of
each transport type to create a different storyline.
i. To begin the middle()
of the story, we print a message to the player to confirm their choice of transport and share some details about it. In the first line of middle()
, print the following message to the player: "Travelling by {transportation.name} is {transportation.speed}. {transportation.description}\n"
(Hint: you will need to use an f-string).
ii. Next call the encounter_obstacles()
method of the transportation
object that
you passed into middle()
method.
iii. Lastly, call the end()
method which will trigger the end of the story.
We haven't yet created any obstacles for the player, we will do this in Section 6.
SOLUTION - MIDDLE
1 def middle(self, character, transportation):
2 print(
3 f"Travelling by {transportation.name} is {transportation.speed}. {transportation.description}\n"
4 )
5 transportation.encounter_obstacles()
6 self.end(character)
Run the program and see what happens!
5. End of the story
We will reach the end of the story when the player's character has reached the lake. When this happens, we will print a message to the player and the game will end.
i. Print a message to the player, which says: "Yay, {character.name}! You reached the lake by {character.transportation.name}. Time to swim and spend the rest of the day snoozing in the sun ⛵"
SOLUTION - END
1 def end(self, character):
2 print(
3 f"Yay, {character.name}! You reached the lake by {character.transportation.name}. Time to swim and spend the rest of the day snoozing in the sun ⛵"
4 )
Run the program and see what happens!
6. Fill in the obstacles of the story
Each story will have some obstacles, and depending on the player's choice when encountering these obstacles, the character will or will not make it to the lake.
Walk
There is one obstacle when walking. Since it is a long walk from Hermannplatz, if the player leaves too late it will be dark when they arrive.
i. Write code to ask the player the following question: "What time do you leave Hermannplatz? a. Before 14:00 b. After 14:00\n"
. Assign their input to a variable
called choice
.
ii. If the choice
is a
, print the following message: "You are very tired when you get to the lake and fall asleep.\n While you are asleep a wild boar runs off with your belongings 🐗"
iii. If the choice
is b
, print the following message: "Oh no, it is too late. By the time you reach the lake, it is already dark and time to go home. 🌉 :("
and exit
the program.
Question: what happpens if the choice is neither a nor b?
SOLUTION - WALK OBSTACLES
1 def encounter_obstacles(self):
2 choice = input(
3 "What time do you leave Hermannplatz? a. Before 14:00 b. After 14:00\n"
4 )
5 if choice == "a":
6 print(
7 "You are very tired when you get to the lake and fall asleep.\n While you are asleep a wild boar runs off with your belongings 🐗"
8 )
9 if choice == "b":
10 print(
11 "Oh no, it is too late. By the time you reach the lake, it is already dark and time to go home. 🌉 :("
12 )
13 exit()
Bicycle
There are two obstacles that the player can encounter while cycling: the cobblestones of Berlin and seeing your friends having fun in Neukölln.
i. Write code to ask the player the following question: "You are cycling past the Landwehr canal and spot your friend in a boat on the canal.\nWhat do you do? a. Stop and join the boat party or b. wave and continue\n"
. Assign their input to a variable
called choice
.
ii. If the choice
is a
, print the following message: "Who needs the lake? Put on some sunglasses and bob along the canal all day.🚣"
and exit
the program.
iii. If the choice
is b
, do nothing and let the program continue.
iv. Write code to ask the player the following question: "Uh oh, another cobble stone road gives you a flat tire.\nWhat do you do? a. Fix it b. Go home\n"
. Assign their input to a variable
called choice
.
v. If the choice
is a
, do nothing and let the program continue.
vi. If the choice
is b
, print the following message: "That's all the adventure for today! The lake will have to wait for another day"
and exit
the program.
SOLUTION - BICYCLE OBSTACLES
1 def encounter_obstacles(self):
2 choice = input(
3 "You are cycling past the Landwehr canal and spot your best friend in a boat on the canal.\nWhat do you do? a. Stop and join the boat party or b. wave and continue\n"
4 )
5 if choice == "a":
6 print(
7 "Who needs the lake? Put on some sunglasses and bob along the canal all day.🚣"
8 )
9 exit()
10 if choice == "b":
11 pass
12
13 choice = input(
14 "Uh oh, another cobble stone road gives you a flat tire.\nWhat do you do? a. Fix it b. Go home\n"
15 )
16 if choice == "a":
17 pass
18 if choice == "b":
19 print(
20 "That's all the adventure for today! The lake will have to wait for another day"
21 )
22 exit()
S-Bahn
There is one obstacle when on the S-Bahn: the player misses there stops and ends up at Erkner.
i. Write code to ask the player the following question: "You miss your stop and end up in Erkner. What do you do? a. Stay there b. Get back on the s-bahn\n"
. Assign their input to a variable
called choice
.
ii. If the choice
is a
, print the following message: "Great! Spend the rest of the day exploring the sights and sounds around Erker!"
and exit
the program.
iii. If the choice
is b
, do nothing and let the program continue.
SOLUTION - WALK OBSTACLES
1 def encounter_obstacles(self):
2 choice = input(
3 "You miss your stop and end up in Erkner. What do you do? a. Stay there b. Get back on the s-bahn\n"
4 )
5 if choice == "a":
6 print(
7 "Great! Spend the rest of the day exploring the sights and sounds around Erker!"
8 )
9 elif choice == "b":
10 pass
7. Run the program
Ta-da, you have finished developing the program! Now trying playing the game.
8. Follow-up ideas
- Add another mode of transport that the player can take
- Add more obstacles and choices for the player for each mode of transport
- Use the things you've learned to make an entirely different story
Continue Learning
- Get involved with the Python Community. Organisations and meetups: PyLadies Berlin, PyBerlin and more.