Skip to article frontmatterSkip to article content

Exercise 7 - Functions

5 total points

Directions

  • Due: 10/19/2025 @ 11:59 p.m.

  • Change the name of your notebook to EX7_FirstLast.ipynb, where First is your first name and Last is your last name.

  • For each of the following prompts, write or modify Python code that fulfills the requirements.

Basic functions

Problem 1

Call the function named p1 below (0.5 pts):

def p1():

    print("You called the function!")

Problem 2

Call the function named p2 below (0.5 pts):

def p2(x):
    print("You called the function with a parameter named that equals x", x)

Problem 3

Call the function named p3 below (0.5 pts):

def p3(x, y):
    print("You called the function with a parameter named x that equals", x, "and a parameter named y that equals", y)

Problem 4

Call the function named p4 below, but do not set the default parameter during your call (0.5 pts):

def p4(x, y=5):
    print("You called the function with a parameter named x that equals", x, "and the default parameter named y that equals 5 by default and equals", y, "in your call.")

Problem 5

Call the function named p5 below using the defined parameter values param1 and param2 for the parameters x and y, respectively, and set the result of the function equal to a variable named x. Do not change any other line except the x = None line (0.5 pts)

def p5(x, y):

    return x*y

param1 = 5
param2 = 3

# only change this line
x = None


print(f"The result of the function p5({param1}, {param2}) is {x}")
The result of the function p5(5, 3) is None

Problem 6

Finish the function named p6 that returns the formatted version of a float parameter with 1 decimal named F with the F unit for Fahrenheit attached to the end of the string (0.5 pts).

For example:

  1. Parameter of 80.52, the function should return 80.5 F.

  2. Parameter of 15.52, the function should return 15.5 F.

def p6():

    return "15.5 F"

temperature = 80.52
x = p6()

print(f"Input={temperature}, output={x}")
Input=80.52, output=15.5 F

Problem 7

Create a function named p7 that follows these requirements (2 pts):

  1. It takes one parameter named CAPE.

  2. For all cases, print "CAPE is XXXX j kg-1", where XXXX is the value of the CAPE parameter. For example, if the CAPE parameter is 50, print "CAPE is 50 j kg-1". CAPE must be an int when printed, even if a float is passed in as a parameter.

  3. If CAPE is greater than or equal to 2000, add (Extreme Instability). to the end of the string.

  4. If CAPE is greater than or equal to 1000 but less than 2000, add (Moderate Instability). to the end of the string.

  5. If CAPE is greater than 0, but less than 1000, add (Low Instability). to the end of the string.

  6. If CAPE is 0, add (No Instability). to the end of the string.

  7. If CAPE less than 0, add (Invalid. Check Input). to the end of the string.

Example output:

CAPE is 2000 j kg-1 (Extreme Instability).
CAPE is 1000 j kg-1 (Moderate Instability).
CAPE is 0.01 j kg-1 (Low Instability).
CAPE is 0 j kg-1 (No Instability).
CAPE is -10 j kg-1 (Invalid. Check Input).