PY-01: Python basics, functions, and string formatting
Read the task files below or download the starter files. Use the Canvas assignment to open CodeGrade and submit your work.
Python files
hello.py
Listing 1 shows the contents of hello.py.
greeting.py
Listing 2 shows the contents of greeting.py.
basic_math.py
Listing 3 shows the contents of basic_math.py.
basic_math.py
def add_two(a, b):
"""Return the sum of two numbers.
Parameters
----------
a : int or float
The first number.
b : int or float
The second number.
Returns
-------
int or float
The sum of the two numbers.
"""
pass # Replace this line with the correct expression
def multiply_two(a, b):
"""Return the product of two numbers.
Parameters
----------
a : int or float
The first number.
b : int or float
The second number.
Returns
-------
int or float
The product of the two numbers.
"""
pass # Replace this line with the correct expressionformat_quote.py
Listing 4 shows the contents of format_quote.py.
format_quote.py
def format_quote(
phrase: str,
author: str,
padding: int
):
"""
Format a phrase as a quotation with equal padding on both sides.
The returned text has the form `"phrase" --- author`, preceded and
followed by the requested number of spaces.
Parameters:
----------
phrase: str
The phrase to be quoted.
author: str
The author of the phrase.
padding: int
The amount of space (number of characters)
before and after the quote.
Returns:
-------
str
The formatted quotation with equal padding on both sides.
Example:
--------
>>> format_quote("To be or not to be", "Shakespeare", 10)
' "To be or not to be" --- Shakespeare '
"""
pass # ADD CODE HERE!format_email.py
Listing 5 shows the contents of format_email.py.
format_email.py
def format_email(
title: str,
greeting: str,
recipient: str,
body: str,
ending: str,
signature: str
):
"""
Format an email message using the specified components.
Separate the title, greeting and recipient, body, and closing with
blank lines. Put the signature on the line after the closing.
Parameters:
----------
title: str
The title of the email.
greeting: str
The greeting line of the email.
recipient: str
The recipient's name.
body: str
The main content of the email.
ending: str
The closing line of the email.
signature: str
The signature to end the email.
Returns:
-------
str
A formatted email message as a multiline string.
Example:
--------
>>> print(format_email("Meeting Request", "Dear", "Alice",
... "Please attend the meeting at 10 AM.",
... "Best Regards", "Bob"))
Meeting Request
Dear Alice,
Please attend the meeting at 10 AM.
Best Regards,
Bob
"""
pass # ADD CODE HERE!format_entry.py
Listing 6 shows the contents of format_entry.py.
format_entry.py
def format_entry(
name: str,
age: int,
weight: float,
name_width: int=20,
age_width: int=5,
weight_width: int=8
) -> str:
"""
Format a name, age, and weight into a string with aligned columns.
The output is suitable for tabular display.
Parameters:
----------
name: str
The name of the person.
age: int
The age of the person.
weight: float
The weight of the person.
name_width: int (default=20)
The width of the name column.
age_width: int (default=5)
The width of the age column.
weight_width: int (default=8)
The width of the weight column.
Returns:
-------
str
Formatted string with each field within its specified
width, separated by vertical bars.
Value alignment within width:
- name: left aligned.
- age: center aligned.
- weight: right aligned with two digits after the decimal point.
Example:
--------
>>> format_entry("Alice", 30, 65.5)
'|Alice | 30 | 65.50|'
>>> format_entry("Alice", 30, 65.5, age_width=6)
'|Alice | 30 | 65.50|'
>>> format_entry("Alice", 30, 65.5, age_width=8)
'|Alice | 30 | 65.50|'
"""
pass # ADD CODE HERE!