Skip to main content

Section 3.9 Functions Mixed-Up Coding Practice

Try to solve each of the following. Click the Check button to check each solution. You will be told if your solution is too short, has a block in the wrong order, or you are using the wrong block.
These are exercises are adapted from Dr. Barb Ericson’s exercises in the AP CSP and Python for Everybody ebooks on Runestone.

Activity 3.9.1.

Construct a block of code with two functions. The first function is called printFlavors, which lists the available flavors. The second function should print the products and call the first function. Finally, call the second function. Watch your indentation! Hint: there is one unused code block.

Activity 3.9.2.

Drag the blocks from the left and put them in the correct order on the right to define a function print_greeting that asks for your name and prints β€œHello Name”. Then define a main function that calls print_greeting. Be sure to also call the main function. Note that you will have to indent the lines that are in the body of each function. Click the Check button to check your solution.

Activity 3.9.3.

Create a function called exp that takes in two parameters, num1 and num2, and returns num1 raised to the power of num2. For example, exp(2,3) should return 8.

Activity 3.9.4.

Create a function called poly_perimeter that takes in two parameters, len_side and num_sides, and returns the perimeter of the polygon. The perimeter of a polygon is the length of each side times the number of sides. For example, poly_perimeter(4,5) should return 20.

Activity 3.9.5.

Create a function called convert_to_miles that takes in feet as a parameter and returns the value in miles. feet contains the length of an object in feet. 1 mile is equal to 5280 feet.

Activity 3.9.6.

Create a function called larger_string that takes in two parameters, str1 and str2, and returns the string that has the larger length. If the lengths are equal, return str1.

Activity 3.9.7.

Create a function called even_or_odd that takes in a parameter num. If num is odd, return "This is odd.", and if num is even, return "This is even." ``. For example, ``even_or_odd(-65) should return "This is odd.". Note: For this function, num is solely an integer.

Activity 3.9.8.

Arrange the blocks to define a function cropped_photo that takes the height and width of a photo. If the height and width are equal, then the function returns "Cropped Photo". Otherwise, it returns "The photo needs to be cropped."

Activity 3.9.9.

Arrange the blocks to define a function bonus that takes two parameters, years and quality. If years is more than 5, then the person gets a bonus. The person also gets a bonus if their quality of work was more than 90. They can also get a bonus if they worked for at least 3 years and had quality more than 80. If the person is eligible for a bonus, return "Eligible for Bonus". Otherwise, return "Ineligible for Bonus".

Activity 3.9.10.

Arrange the blocks to define a function move_elevator that takes two parameters, current_floor and next_floor. If the elevator moves to a floor above, then it should return "Up". Otherwise, it should return "Down". Also, if next_floor is the same as current_floor, it should return 0.

Activity 3.9.11.

Arrange the blocks to define a function lunch_break that takes a parameter class_standing and determines how long a lunch break will be based on the student’s class standing. If a person is a β€˜Freshman’, return 40. If a person is a β€˜Sophomore’, return 30. If a person is a β€˜Junior’, return 20. If a person is a β€˜Senior’, return 15. If a person is none of these, return 0.

Activity 3.9.12.

Create a function called countdown that takes in one integer parameter seconds and creates a list of numbers that counts down from seconds to 1, and then returns that list. Note: seconds must be greater than or equal to 1. For example, countdown(3) would return [3, 2, 1].
You have attempted of activities on this page.