Skip to main content

Exercises 6.17 Write Code Questions

1.

Fix the 4 errors so the following code runs and returns the perimeter of a rectangle. For example, recPerimeter(10, 20) should return 60.
Solution.
# Function headers end with a colon (:)
def recPerimeter(length, width):

    # Indent four spaces after the header
    perimeter = 2 * (length + width)

    # Keyword return is lowercase
    # Return the variable, not the function name
    return perimeter

2.

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

3.

Change the code so that areaTriangle takes parameters for the base and height of a triangle and computes its area. Then, write code to call the function and print the result. For example, areaTriangle(12, 45) should return 270.
Solution.
# Give function base and height arguments
def areaTriangle(base, height):
    # Set area variable to the formula using arguments
    area = (base * height) / 2
    # Return the calculated area
    return area
# Call the function with arguments
print(areaTriangle(2, 6))

4.

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.

5.

Fix the errors on line 2 so the function nameAndAge returns the string “My name is nameString and I am ageInt years old.” For example, nameAndAge("John", 18) should return "My name is John and I am 18 years old."
Solution.
def nameAndAge(nameString, ageInt):
    # Use quotes around strings, keep variables outside of quotes
    # Include spacing so variables are not connected to words
    return("My name is " + nameString + " and I am " + str(ageInt) + " years old.")

print(nameAndAge("John", 18))

6.

Write a program that uses a function called compute_grade that takes a score as its parameter and returns a string representing a grade. If someone a score greater than 1, return 'Bad score'. For example, compute_grade(.95) should return 'A'.
Table 6.17.1.
Score Grade
>= 0.9 A
>= 0.8 B
>= 0.7 C
>= 0.6 D
< 0.6 F

7.

Write a value-returning function sum_to(n) that returns the sum of all integer numbers up to and including n. For example, sum_to(10) would compute 1 + 2 + 3 + … + 10 and return the value 55. Use this equation to find this sum: \(n(n+1) \over 2\text{.}\) For example, sum_to(15) should return 120.0.
Solution.
def sumTo(n):
    # set result variable to given equation
    result = (n * (n + 1)) / 2
    # return the variable
    return result
    # alternatively, you can accomplish this on one line:
    # return (n * (n + 1)) / 2

8.

Write a function circle_area(r) which returns the area of a circle of radius r. Make sure you import the math module in your solution to obtain an accurate value of pi. For example, circle_area(31415.926535897932) should return 3100627668.0299816.
Solution.
# Import the math module
import math

def circle_area(r):
    # Use ** to square r, then multiply by pi
    a = r**2 * math.pi
    # Return the area
    return a

9.

Finish the function to return the average of three numbers, but drop the lowest value. For example, get_avg_drop_lowest(100, 10, 0) returns 55 and get_avg_drop_lowest(4, 2, 10) returns 7. Hint: use the min function to find the lowest value.

10.

Write the check_guess function below which computes if a guess is too low, too high, or correct. Return 'too low' if guess is less than target, 'correct' if they are equal, and 'too high' if guess is greater than target. For example, check_guess(5, 7) returns 'too low', check_guess(7, 7) returns 'correct', and check_guess(9, 7) returns 'too high'.