What you will learn
- Use project planning skills to complete a longer-term project
- Create functions to organize a project
- Apply skills learned in units 1-3 to create a functioning program
Project
Using variables, functions, and conditionals in Python, students will create an Oregon Trail game.
Overview
We will be recreating Oregon Trail! The goal is to travel from Independence, Missori to Oregon City, Oregon (2000 miles) by Dec 31st. However, the trail is arduous. Each day costs you food and health. You can hunt and rest, but you have to get there before winter!
Details
Behavior
- Player starts in Independence, MO on 03/01 with 2,000 miles to go, 500lbs of food, and 5 health.
- The player must get to Oregon by 12/31
- At the beginning of the game, user is asked their name.
- Each turn, the player is asked what action they choose, where the player can type in the following:
travel,rest,hunt,status,help,quit - On average, the player’s health will randomly decrease twice during a month on any given day.
- The player eats 5lbs of food a day.
travel: moves you randomly between 30-60 miles and takes 3-7 days (random).rest: increases health 1 level (up to 5 maximum) and takes 2-5 days (random).hunt: adds 100 lbs of food and takes 2-5 days (random).status: lists food, health, distance traveled, and day.help: lists all the commands.quit: will end the game.
Implementation details
- Create functions for all options a player can take
- Use globals to keep track of player health, food pounds, miles to go, current day, current month
- Create a function add_day which updates the day
- Use global list to keep track of which months have 31 days and use this in the add_day function (i.e.: MONTHS_WITH_31_DAYS = [1, 3, 5, 7, 8, 10, 12])
- Create a function select_action which uses a while loop to call add_day function
Bonus
- Make the rate of food consumption be a function of activity. So if a player hunts for a turn they take up more food, but if they rest they take up less food.
- Create a random event that occurs randomly once a month, like a river crossing or a dysentery, that will take up a range of 1-10 food, 1-10 days and 0-1 health.
Alternate Project
Created by Brian Weinfeld
Using variables, functions, and conditionals in Python, you will create a daily planner program.
Overview
In this project, you will be creating a daily planner program. A daily planner helps a user track what they need to do today and when they need to do it. This can be thought of as a TODO list that pairs each action with a time when the action is done. A planner can help make sure a person doesn’t accidentally book two things for the same time. Do you add events to your phone’s calendar?
Details
Behavior
Welcome to your Daily Planner
What would you like to do? (add, clear, display, exit) add
What event would you like to add? go to school
What time? 8
Is this event critical? no
Added go to school at 8
['', '', '', '']
['', '', '', '']
['go to school', '', '', '']
['', '', '', '']
['', '', '', '']
['', '', '', '']
What would you like to do? (add, clear, display, exit) add
What event would you like to add? wake up
What time? 6
Is this event critical? no
Added wake up at 6
['', '', '', '']
['', '', 'wake up', '']
['go to school', '', '', '']
['', '', '', '']
['', '', '', '']
['', '', '', '']
What would you like to do? (add, clear, display, exit) add
What event would you like to add? do homework
What time? 6
Is this event critical? no
Conflict with wake up
What would you like to do? (add, clear, display, exit) display
['', '', '', '']
['', '', 'wake up', '']
['go to school', '', '', '']
['', '', '', '']
['', '', '', '']
['', '', '', '']
What would you like to do? (add, clear, display, exit) add
What event would you like to add? eat breakfast
What time? 6
Is this event critical? yes
Replacing wake up at 6 with eat breakfast
['', '', '', '']
['', '', 'eat breakfast', '']
['go to school', '', '', '']
['', '', '', '']
['', '', '', '']
['', '', '', '']
What would you like to do? (add, clear, display, exit) clear
What time to start clearing? 5
What time to end clearing? 7
Clearing ['', 'eat breakfast']
['', '', '', '']
['', '', '', '']
['go to school', '', '', '']
['', '', '', '']
['', '', '', '']
['', '', '', '']
What would you like to do? (add, clear, display, exit) exit
Goodbye
Implementation Details
The planner is a list that has 24 empty strings in it. (The empty string is ‘’). Each of the 24 indexes represents 1 hour of time in a day. For example, index 0 represents 12:00AM to 12:59AM, index 1 represents 01:00AM to 01:59AM and index 23 represents 11:00PM to 11:59PM. Your program will allow a user to add events at certain times of the day and prevent conflicts from occurring. All of the events will take exactly 1 hour.
Think back to the previous project where you created a TODO list program. All of the code in that program was put into a single script. This is OK when the program is small, but it gets harder and harder to code as the project gets bigger. Did you find the end of that project to be more challenging than the beginning? You can help mitigate this problem by creating functions to handle large amounts of work.
Begin this project by creating your 24 length list of empty strings and the following three functions. Do your best to ensure that these functions work properly before moving onto the rest of the project.
def add_event(planner, event, time, critical):
# finish function
- add_event takes the planner (your list), the event you wish to add (a string), the time you want to do the event (an int between 0 to 23 inclusive) and whether the event is critical (a boolean). If there is no event in your planner for the indicated time, add it to the planner. If there is already an event for that time you have a conflict! (You can’t do both things at the same time.) Use critical to figure out what to do. If critical is True, then the new event takes priority. Replace the event in the planner at that time with the new event. If critical is False, the event is not more important and instead an error should be printed. No change is made to the list. Return True if a new event is added to the planner and False if no changes were made.
def display_planner(planner):
# finish function
- display_planner takes the planner (your list) and displays it in an easy to read fashion. Print 6 lines, each line containing 4 events. So the first line will contain events from the first four indexes, the second line will contain the next four and so on.
def clear_timeframe(planner, timeframe):
# finish function
-
clear_timeframe takes the planner (your list) and a timeframe (slice) that you wish to clear. Sometimes you need to remove events that have been cancelled. Clear out all of the events that occur during the given timeframe. Be careful, a slice can contain multiple elements. You need to set all the events in the slice back to the empty string.
-
Once you have finished the above functions, create a script that allows a user to add and remove elements from their planner. The user should have four choices add, clear, display, exit. add is used to add events to the planner, clear is used to remove events from the planner, display is used to display the day’s events and exit is used to exit the program. You should also automatically print the planner after any changes have been made to it. See the below running example for details.
Challenge
This section contains additional components you can add to the project. These should only be attempted after the project has been completed.
-
Some events take longer than one hour. Modify the add_event function so that time is now a slice representing the beginning and ending time of the event. You will need to place the event in each index in your planner. Be careful, the new event can only be placed if either the event is critical (overwriting any event already in the planner at that time) or every hour in the planner is empty.
-
Combine all the information into one line so that it is easier to interact with the planner. For example, you might add a new event by entering “add go to school 8 13 critical” or “add practice lines 14 15 not”
Super Challenge
The super challenge will require knowledge that has not been taught yet. You will need to do additional research on your own. Good luck!
Sometimes the time the event is scheduled for is less important than making sure the event is on the schedule. Add a parameter to the functions that add an element to the schedule called force. If force is true, the event must be scheduled. If it can’t be scheduled at the expected time, find the closest available timeslot in the future and schedule it there instead. If the event is critical, then the displaced event should be moved forward. Be sure to alert the user that the event has been scheduled, but at a different time than expected! The placement will still fail if there are no available timeslots to place the event.
Rubric
Student Name: __________ Date: _________
Student correctly identifies data types (2.01)
| 3 | 2 | 1 | 0 | Points |
|---|---|---|---|---|
| Student correctly identifies all data types | Student correctly identifies most data types | Student identifies a few data types | No evidence that the student can correctly identify data types | |
| Sub Total |
Student correctly uses lists (2.04, 2.05)
| 3 | 2 | 1 | 0 | Points |
|---|---|---|---|---|
| Student programs uses lists of multiple types | Student program uses lists of one type | __ | No evidence that they student can create a list using multiple types | |
| Sub Total |
Student correctly uses built in functions (3.01)
| 3 | 2 | 1 | 0 | points |
|---|---|---|---|---|
| Student correctly uses a built in function that returns a value | Student uses a built in function, but only prints that value | Student uses build in functions incorrectly | No evidence the student can use built in functions | |
| Sub Total |
Student can program using user-defined functions (3.02, 3.03, 3.04)
| 3 | 2 | 1 | 0 | points |
|---|---|---|---|---|
| Student created at least four user designed functions with the correct syntax | uses created 3 user-defined functions with the correct syntax | Student creates one or two user- defined functions with correct syntax | No evidence the student can program user-defined functions | |
| Student’s user-defined functions always decrease the complexity of the code | Student’s user-defined functions usually decrease the complexity of the code | Student’s user-defined functions rarely reduce complexity | No evidence the student can create user-defined functions to decrease complexity | |
| Student always uses a returned value correctly | Student usually uses a returned value correctly | Student sometimes uses a returned value correctly | No evidence the student understands how to use the a returned value | |
| Student always scopes their variables correctly | Student usually scopes their variables correctly | Student sometimes scopes their variables correctly | No evidence the student understands how to scope their variables | |
| Sub Total |
Student can decompose a problem to create a program from a brief
| 3 | 2 | 1 | 0 | Points |
|---|---|---|---|---|
| Student program runs without error | The students program has a few errors, but it does not impact the program’s functionality | Student program has errors that impact the program’s functionality | Student program is not functional | |
| __ | Students submitted documentation showing planning for most variables and functions. | Students submitted documentation showing planning for a few variables and functions. | No evidence of planning | |
| Sub Total |
Student uses naming/ syntax conventions and comments to increase readability
| 2 | 1 | 0 | Points |
|---|---|---|---|
| Syntax conventions are generally used | Sometimes syntax conventions are used | No evidence of syntax conventions to aid in code readability | |
| All variables have clear names | Some variables have clear names | No evidence of using variable names to aid in code read ability | |
| __ | Student comments aid code readability | No evidence of using comments to aid in code readability. | |
| Sub Total |
Final Grade
| Points Possible | Points Earned x Weight | Total Points |
|---|---|---|
| 31 | __ X ___ |
Check your understanding
Pick an answer for each question. You can try again until you get it right.
1. Why is it helpful to split a longer program into functions?
- Python requires it
- It makes the program run faster
- Each function does one job, so the code is easier to read, test, and reuse
- It removes the need for variables
Functions organize code and decrease its complexity.
2. Why plan out your variables and functions before writing code?
- It helps you decompose the problem and catch design problems early
- It makes comments unnecessary
- It is only needed for programs under 10 lines
- It guarantees there will be no bugs
Planning helps you break a larger problem into smaller pieces before coding.
3. A function’s contract says Returns: none. What does that tell you?
- It returns a number
- It returns the string
none - It cannot use any arguments
- It does not give back a value; it only does something such as printing or updating data
A function that returns none is used for its effects, not for a result.