Workshop 7 focuses on three themes: working with imports, designing flexible function signatures, and building introductory classes. Keep your solutions in a single file (for example, homework_7.py) so you can run them end to end.
- Create a module-level constant
PI_TWO_DECIMALSusingmath.pirounded to two decimal places. - Import
datetimeasdtand write acurrent_utc()helper that returns the current UTC timestamp usingdt.datetime.now(dt.timezone.utc). - From
random, importrandintasrolland implement aroll_die(sides=6)function that returns a random integer between 1 andsides. - Print a short summary showing the constant, current time, and the result of two dice rolls.
- Write
announce(event, *audiences, prefix="[INFO]")that formats a message similar to the example inworkshop_7/arguments_examples.py. - Implement
average_score(*scores)that raises aValueErrorwhen called without scores, otherwise returns the mean rounded to one decimal place. - Create
build_connection(host, *, port=5432, **credentials)that merges the requiredhost, keyword-onlyport, and any extra credentials into a dictionary. - Demonstrate each function with at least two sample calls covering different argument combinations.
- Define
enroll_student(name, /, *, course, level="beginner", **meta). - Call the function three times showing how positional-only, keyword-only, and extra metadata arguments behave. Print the returned dictionaries so the structure is clear.
- Add one comment explaining why the positional-only marker (
/) is useful in this context.
- Create a
Courseclass that storestitle,capacity, and a list of students. Add anenroll(student)method that raisesValueErrorif the course is full and aroster()method that returns the names joined by commas or"No students yet"when empty. - Subclass it into
OnlineCoursethat adds aplatformattribute and aninfo()method returning a string like"Python Foundations on Zoom (2/25 students)". - Instantiate both classes, enroll a handful of students, and print
repr(course), the roster, and the info message to verify everything works.
- Combine the pieces by writing
course_report(*courses, **filters)that uses the earlier helpers (such asannounce) to format a summary. Keep it simple: filtering by platform or minimum capacity is enough.