Introduction to Python
Assignments for Week 2
Assignment 1
In a popular TV show, a bank has a policy to give customers $100 if their staff fails to greet them with “hello.” A character named Kramer is greeted with “hey” instead of “hello,” prompting him to claim the $100. The bank’s manager offers a compromise, saying, “You received a greeting starting with ‘h’, how about $20?” Kramer agrees to the offer.
Create a Python program that checks the greeting provided by the user and determines the amount of money they would receive based on the following rules.
Implement a program in a file called bank.py
that prompts the user for a greeting and outputs an amount of money based on the following criteria:
- If the greeting starts with “hello”, output
$0
. - If the greeting starts with an “h” (but not “hello”), output
$20
. - Otherwise, output
$100
.
Hints
- As you know, a
str
comes with different methods, per docs.python.org/3/library/stdtypes.html#string-methods. - Be sure to give $0 not only for “hello” but also “hello there”, “hello, Newman”, and anything like that.
The output of your program should look like this:
Assignment 2
Although Windows and macOS sometimes conceal them, most files have extensions, which are suffixes starting with a period (.
) at the end of their names. For example, the file names for GIFs end in .gif, and the file names for JPEGs end in .jpg or .jpeg. When you double-click a file to open it, your computer uses its extension to decide which program to launch.
In contrast, web browsers use media types (previously known as MIME types) to determine how to display files found on the web. When you download a file from a web server, the server sends an HTTP header with the file, indicating its media type. For instance, the media type for a GIF is image/gif, and for a JPEG, it is image/jpeg. To identify the media type of a file, a web server usually looks at the file’s extension and maps it accordingly.
Refer to Common MIME types on MDN Web Docs for a list of common types.
In a file called extensions.py
, create a program that prompts the user for a file name and then outputs the file’s media type if the name ends, case-insensitively, with any of these suffixes:
- .gif
- .jpg
- .jpeg
- .png
- .txt
- .zip
If the file name ends with a different suffix or has no suffix at all, output application/octet-stream as a default.
The output of your program should look like this:
Assignment 3
Python already supports various mathematical operations such as addition, subtraction, multiplication, and division. However, let’s create a program that allows users to perform these operations without needing to know Python.
In a file called interpreter.py
, write a program that prompts the user for an arithmetic expression and then calculates and displays the result as a floating-point number formatted to one decimal place. Assume the user’s input will be formatted as x y z, with one space between each element, where:
- x is an integer
- y is one of the operators: +, -, *, or /
- z is an integer
For example, if the user inputs 1 + 1
, your program should output 2.0
. You can assume that if y is /, z will not be 0.
Just as Python itself acts as an interpreter for Python code, your interpreter.py
will act as an interpreter for basic arithmetic expressions.
Hints:
Recall that a string (str) in Python has various methods, as detailed in the official documentation, including split
, which divides a string into a list of substrings. For example, if expression
is a string like 1 + 1
, then:
x, y, z = expression.split(" ")
will assign 1
to x
, +
to y
, and 1
to z
.
The output of your code should look like this:
Assignment 4
Imagine you are in a country where it’s customary to eat breakfast between 7:00 and 8:00, lunch between 12:00 and 13:00, and dinner between 18:00 and 19:00. Wouldn’t it be useful to have a program that tells you what meal to eat and when?
In meal.py
, create a program that prompts the user for a time and outputs whether it’s time for breakfast, lunch, or dinner. If it’s not time for a meal, the program should not output anything. Assume the user’s input will be in 24-hour format as #:## or ##:##. Also, assume that each meal’s time range is inclusive. For example, if the time is 7:00, 7:01, 7:59, or 8:00, or any time in between, it’s breakfast time.
Structure your program as shown below. The convert
function should be called by main
and will convert the time, provided as a string in 24-hour format, into the corresponding number of hours as a float. For example, if given “7:30” (i.e., 7 hours and 30 minutes), convert
should return 7.5 (i.e., 7.5 hours).
def main():
...
def convert(time):
...
if __name__ == "__main__":
main()
Hints
- Recall that a
str
comes with quite a few methods, per docs.python.org/3/library/stdtypes.html#string-methods, includingsplit
, which separates astr
into a sequence of values, all of which can be assigned to variables at once. For instance, iftime
is astr
like"7:30"
, thenhours, minutes = time.split(":")
will assign “7” to hours and “30” to minutes. - Keep in mind that there are 60 minutes in 1 hour.
The output of your program should look like the following: