Skip to main content

Section 3.10 Functions Coding Practice

These coding exercises are adapted from the Python for Everybody ebook by Dr. Barb Ericson on Runestone.

Activity 3.10.1.

Write a function called welcome_message that takes in a parameter name and prints "Hello (name) :-)". For example, welcome_message('Aurora') should return "Hello Aurora :-)".

Activity 3.10.2.

Write a function called area that takes in parameters width and length and returns the area (of the rectangle). For example, area(5,4) should return 20.

Activity 3.10.3.

Write a function called address that combines 3 different string address parameters (city, state, and zip) and returns a user’s address. After the city and state inputs, add a comma and a space. For example, address('Seattle', 'WA', '98105') should return "Seattle, WA, 98105".

Activity 3.10.4.

Fix the 4 errors so the following code runs and returns the perimeter of a rectangle. For example, recPerimeter(10, 20) should return 60.

Activity 3.10.5.

Fix the 5 errors so the following code runs and returns the area of a square. For example, squareArea(10) should return 100.

Activity 3.10.6.

Change the code so that areaTriangle takes parameters for the base and height of a triangle and computes its area. For example, areaTriangle(12,45) should return 270.

Activity 3.10.7.

Change the code below to create a function tripCost that calculates the cost of a trip. It should take the miles, milesPerGallon, and pricePerGallon as parameters and should return the cost of the trip. For example, tripCost(100, 25, 2.24) should return 8.96.

Activity 3.10.8.

Write a function called convert that takes in feet as a parameter and returns the value in miles. 1 mile is equal to 5280 feet.

Activity 3.10.9.

Write a function called average(num1, num2), which finds the average of two numbers num1 and num2. For example, average(10,4) should return 7.0.

Activity 3.10.10.

Write a function called bigger(p1, p2) which returns whichever is greater of its two parameters.

Activity 3.10.11.

Write a function called even_or_odd that takes in a parameter num. If num is odd, return "odd", and if num is even, return "even". For example, even_or_odd(-65) should return "odd". Note: For this function, num is solely an integer. Remember that you can use % 2 == 0 to check if a number is even with no remainder when divided by 2.

Activity 3.10.12.

Write a function called list_sum that takes in a parameter num_list, which is a list of numbers. The function should return the sum of all the numbers in the list. For example, list_sum([1, 2, 3]) should return 6. Hint: Use a for loop to iterate through the list and add each number to a total sum variable.
You have attempted of activities on this page.