Fix the 4 errors so the following code runs and returns the perimeter of a rectangle. For example, recPerimeter(10, 20) should return 60.
# 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.
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.
# 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."
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."
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.
Rewrite the grade program from the previous chapter using a function called computegrade that takes a score as its parameter and returns a string representing a grade. If someone enters a string or a score greater than 1, return 'Bad score'. For example, computegrade(.95) should return 'A'.
Score Grade
>= 0.9 A
>= 0.8 B
>= 0.7 C
>= 0.6 D
< 0.6 F
7.
Write a fruitful function sumTo(n) that returns the sum of all integer numbers up to and including n. For example, sumTo(10) would compute 1 + 2 + 3 + … + 10 and return the value 55. Use this equation to find this sum: (n * (n + 1)) / 2. For example, sumTo(15) should return 120.0.
Write a fruitful function sumTo(n) that returns the sum of all integer numbers up to and including n. For example, sumTo(10) would compute 1 + 2 + 3 + … + 10 and return the value 55. Use this equation to find this sum: (n * (n + 1)) / 2. For example, sumTo(15) should return 120.0.
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.
Rewrite the function sumTo(n) that returns the sum of all integer numbers up to and including n. This time, print your answer before you return it. For example, sumTo(15) should return 120.
9.
Write a function areaOfCircle(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, areaOfCircle(31415.926535897932) should return 3100627668.0299816.
Write a function areaOfCircle(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, areaOfCircle(31415.926535897932) should return 3100627668.0299816.
# Import the math module
import math
def areaOfCircle(r):
# Use ** to square r, then multiply by pi
a = r**2 * math.pi
# Return the area
return a
10.
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, 3, 10) returns 7.
11.
You are driving a little too fast, and a police officer stops you. Write code to compute the kind of ticket you'll receive, encoded as an int value: 0 = no ticket, 1 = small ticket, and 2 = big ticket. If speed is 60 or less, return 0. If speed is between 61 and 80 inclusive, return 1. If speed is 81 or more, return 2. If it is your birthday, your speed can be 5 higher in all cases. For example, caught_speeding(60,False) should return 0.
You are driving a little too fast, and a police officer stops you. Write code to compute the kind of ticket you'll receive, encoded as an int value: 0 = no ticket, 1 = small ticket, and 2 = big ticket. If speed is 60 or less, return 0. If speed is between 61 and 80 inclusive, return 1. If speed is 81 or more, return 2. If it is your birthday, your speed can be 5 higher in all cases. For example, caught_speeding(60,False) should return 0.
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'.
13.
Write a function called alarm_clock that has parameters day and vacation. Given a day of the week encoded as 0 = Sun, 1 = Mon, 2 = Tue, … 6 = Sat and a boolean indicating if we are on vacation, return a string indicating when the alarm clock should ring. If we are on vacation and it is a weekend (0 = Saturday or 6 = Sunday), it should return "off", and otherwise return "10:00". If we are not on vacation and it is a weekend, it should return "10:00", and otherwise return "7:00". For example, alarm_clock(1, False) should return '7:00'.
Write a function called alarm_clock that has parameters day and vacation. Given a day of the week encoded as 0 = Sun, 1 = Mon, 2 = Tue, … 6 = Sat and a boolean indicating if we are on vacation, return a string indicating when the alarm clock should ring. If we are on vacation and it is a weekend (0 = Saturday or 6 = Sunday), it should return "off", and otherwise return "10:00". If we are not on vacation and it is a weekend, it should return "10:00", and otherwise return "7:00". For example, alarm_clock(1, False) should return '7:00'.
def alarm_clock(day, vacation):
if vacation:
if (day == 0 or day == 6):
return 'off'
else:
return '10:00'
else:
if not vacation and (day == 0 or day == 6):
return '10:00'
else:
return '7:00'