PY-02: Conditions and decision rules
Read the task files below or download the starter files. Use the Canvas assignment to open CodeGrade and submit your work.
Conditions
These short functions turn rules written in words into decisions your program makes.
What to do
Open each file listed in the editor. Every file holds one function with a docstring that states the rule, and a stub that looks like this:
Listing 1 shows the line to replace in each unfinished function.
Replace that line with your code. Read the Rules section of the docstring before you write anything: it is the specification, and the hidden tests follow it exactly.
Table 1 lists the exercises in this practical.
| File | Write |
|---|---|
even_or_odd.py |
Return 'Even' or 'Odd' for a number. |
reorder_decision.py |
Decide whether stock is above, at, or below its reorder point. |
leap_year.py |
Decide whether a year is a leap year. |
grade_to_letter.py |
Turn a numeric grade into a letter, or report an invalid grade. |
traffic_light.py |
Turn a traffic light colour into an action, using match ... case. |
What is graded
Passing the visible examples is not the same as being correct. Each question is also run against cases you are not shown: values sitting exactly on a boundary, inputs outside the valid range, and capitalisation you may not have considered.
A question counts as complete when every check on it passes.
Check your work in CodeGrade
Select Auto Test, then Run AutoTest, to check your work without creating a submission. Read the feedback, revise, and run it again as often as you need. Select Hand-in your work when you want to create a submission.
Python files
even_or_odd.py
Listing 2 shows the contents of even_or_odd.py.
even_or_odd.py
def even_or_odd(number: int) -> str:
"""
Determine and return whether a number is even or odd.
Parameters
----------
number : int
The number to check. May be negative or zero.
Returns
-------
str
'Even' if the number is even, 'Odd' if the number is odd.
Examples
--------
>>> even_or_odd(4)
'Even'
>>> even_or_odd(3)
'Odd'
"""
raise NotImplementedError("Replace this line with your code.")reorder_decision.py
Listing 3 shows the contents of reorder_decision.py.
reorder_decision.py
def reorder_decision(current_inventory: int, reorder_point: int) -> str:
"""
Determine whether a part needs to be reordered.
Rules
-----
- Above the reorder point: return 'No reorder needed'.
- Exactly at the reorder point: return 'Prepare to reorder'.
- Below the reorder point: return 'Reorder now'.
Parameters
----------
current_inventory : int
The current inventory level of the part.
reorder_point : int
The inventory level at which reordering is required.
Returns
-------
str
'No reorder needed', 'Prepare to reorder', or 'Reorder now'.
Examples
--------
>>> reorder_decision(70, 60)
'No reorder needed'
>>> reorder_decision(60, 60)
'Prepare to reorder'
>>> reorder_decision(50, 60)
'Reorder now'
"""
raise NotImplementedError("Replace this line with your code.")leap_year.py
Listing 4 shows the contents of leap_year.py.
leap_year.py
def is_leap_year(year: int) -> bool:
"""
Determine whether a given year is a leap year.
Rules
-----
A year is a leap year when it is divisible by 4, except for century
years (divisible by 100), which are leap years only when they are also
divisible by 400.
So 2024 is a leap year, 1900 is not, and 2000 is.
Parameters
----------
year : int
The year to check.
Returns
-------
bool
True if the year is a leap year, False otherwise.
Examples
--------
>>> is_leap_year(2024)
True
>>> is_leap_year(2023)
False
>>> is_leap_year(1900)
False
>>> is_leap_year(2000)
True
"""
raise NotImplementedError("Replace this line with your code.")grade_to_letter.py
Listing 5 shows the contents of grade_to_letter.py.
grade_to_letter.py
def convert_grade_to_letter(grade: float) -> str:
"""
Convert a numeric grade to a letter grade.
Rules
-----
A valid grade is a number from 0 to 100 inclusive:
- 'A': 90 to 100
- 'B': 80 to 89
- 'C': 70 to 79
- 'D': 60 to 69
- 'F': below 60
Any number outside 0 to 100, above or below, is not a valid grade and
returns 'Grade is invalid'.
Parameters
----------
grade : float
The numeric grade to convert.
Returns
-------
str
'A', 'B', 'C', 'D', 'F', or 'Grade is invalid'.
Examples
--------
>>> convert_grade_to_letter(92)
'A'
>>> convert_grade_to_letter(85)
'B'
>>> convert_grade_to_letter(58)
'F'
>>> convert_grade_to_letter(105)
'Grade is invalid'
"""
raise NotImplementedError("Replace this line with your code.")traffic_light.py
Listing 6 shows the contents of traffic_light.py.
traffic_light.py
def traffic_light_action(color: str) -> str:
"""
Interpret a traffic light signal and return the action to take.
The colour is recognised whatever its capitalisation, so 'red', 'Red',
and 'RED' all mean the same signal.
Rules
-----
- 'red': vehicles must stop, so return 'Stop'.
- 'yellow': caution, so return 'Slow down'.
- 'green': proceed if the way is clear, so return 'Go'.
- Any other colour is invalid, so return 'Invalid color'.
Parameters
----------
color : str
The colour of the traffic light, in any capitalisation.
Returns
-------
str
'Stop', 'Slow down', 'Go', or 'Invalid color'.
Examples
--------
>>> traffic_light_action('red')
'Stop'
>>> traffic_light_action('YELLOW')
'Slow down'
>>> traffic_light_action('Green')
'Go'
>>> traffic_light_action('blue')
'Invalid color'
"""
raise NotImplementedError("Replace this line with your code.")