Example
Creating Simple Functions
Functions group code for reuse. Defining functions with def keyword.
Showing 51–60 of 78
Example
Functions group code for reuse. Defining functions with def keyword.
Challenge
Refactor the temperature summary: delete the hand-written mean() and median() and use statistics.mean() and statistics.median() instead, then add the standard deviation and handle passes with too few…
Challenge
A call to schedule_launch() passes the pad where the rocket belongs, and the board says a launch pad is flying. Predict the line, find which parameter got which value, fix it with keyword arguments…
Example
Docstrings describe what a function does. Good programming practice.
Example
Parameters allow passing data to functions. Functions become more flexible.
Challenge
…load_config() Move the work into load_config(text) , which returns (config, problems) and never crashes. Text that is not valid JSON is one problem, not valid JSON: and the error's msg ; JSON that is…
Challenge
…that lets bugs through Write parse_pressure(text) , which returns the number, or None when the text is not a number. Catch only ValueError , so a bug such as parse_pressure(None) still raises its…
Challenge
The monitoring system raises the same alert over and over. Group repeats of an alert that come within 60 seconds of the first into one line with a count, in the order they first appeared, then add the…
Challenge
Clean the crew sign-ups for the analogue mission: trim and capitalise names, lower-case emails, then reject sign-ups with no name or a broken email and drop duplicates by email, saying why for each…
Challenge
Extend format_log() from "Log formatter function" with **fields, so any extra keyword arguments are added to the line as key=value pairs, then pass whole dicts of fields into it with ** at the call.