A12 — More Python basics and while loops
Practice using tuples, format strings, conditionals, and while loops.
Due Nov 18, 2026
Estimated time to complete (just the assignment, excluding the prep materials): 40–80 minutes.
Prep materials:
- Week 12: Revisiting conditionals and learning more about loops
- IPyP 3.2 Formatted strings from Week 10: Python basics.
What to do
Create a12.py, following the same file structure as last week (imports at the beginning and a call to pytest at the end).
Submit the file to Assignment 12 Submission on Brightspace. Remember to use the auto-grader, test your Brightspace submission, and send your score to Brightspace when you are done working on the submission.
Remember to write tests even if they are not explicitly required or autograded (you have a better chance of creating a correct solution that way). When raising errors, use ValueError as in the previous assignment.
Essentials
Define a function
hours_minutes(along with a testing functiontest_hours_minutes) that takes a nonnegative integer number of minutes and returns a tuple of two integers: the number of whole hours in it and the number of minutes left over. For example,hours_minutes(125)is(2, 5),hours_minutes(60)is(1, 0), andhours_minutes(0)is(0, 0). Hint.If the given number of minutes is negative, the function should raise an error (
ValueError, as in the previous assignment).For the return type annotation, you could use
tuple[int, int], but such a type can mean a lot of different things (for example, a 2D point on a plane). If we did not need to practice tuples, we would define a data class with two fields (e.g.,hoursandminutes) instead of using tuples. Since we are using tuples, at the very least, let’s define a type alias with the nameTime,and use it in type annotations instead of
tuple[int, int].
Define a function
total_minutesthat does the opposite: it takes a time of day(hours, minutes)(a single value of typeTime) and returns the total number of minutes since midnight. For example,total_minutes((2, 5))is125.Define a function
time_string(along with a testing functiontest_time_string) that takes a time of day(hours, minutes)and returns it as a string in the format"HH:MM", with the hours and minutes always written using two digits:time_string((13, 5))is"13:05", andtime_string((0, 0))is"00:00".- If the hours are not between 0 and 23 or the minutes are not between 0 and 59, the function should raise an error.
- The string must be built with an f-string. Hint.
Define a function
hours_minutes_loopthat behaves the same way ashours_minutesbut uses a while loop and subtraction (without using//,%,divmod, etc.).Define a testing function
test_hours_minutes_loopthat compareshours_minutes_loopwithhours_minuteson each element of a list of several numbers, using a for-loop over that list.
Dig Deeper
A bus line runs all day: the first bus leaves at some time of day, and then a bus leaves every interval minutes. Define a function next_bus that takes:
- the time of the first bus (a single value of type
Timewhere hours and minutes satisfy the same constraints as intime_string; if the time is invalid, the function should raise an error), - the interval (a positive integer number of minutes, with an error raised otherwise), and
- the current time of day (a single value of type
Time, with the same validity constraints as the first bus time).
The function returns either a Time value or None. Use Time | None as the return type annotation.
The time returned is the time on the same day the next bus can still be caught, that is, the first bus that leaves at or after the current time.
If there is no bus that can be caught on the same day, the function should return
None.For example, with the first bus at
(6, 30)and buses every 20 minutes:next_bus((6, 30), 20, (7, 0))is(7, 10),next_bus((6, 30), 20, (6, 50))is(6, 50)(a bus leaving right now counts),next_bus((6, 30), 20, (5, 45))is(6, 30)(nothing runs before the first bus), andnext_bus((6, 30), 20, (23, 51))isNone(the last bus leaves at(23, 50)).
The function must be implemented using a while loop. Think: what would your loop do with an interval of 0 if you did not check for it?
Click here for a hint
The loop starts at the time of the first bus and moves forward one interval at a time. You can usetotal_minutes and hours_minutes.
Code quality check. Look back at your code:
- have you done any copy-pasting that can be avoided by defining a helper function?
- have you used any “magic constants” that could be replaced with named constants?
Grading
Auto-graded for correctness and some aspects of style, with some of the named test functions auto-graded for coverage.