5.16. Write Code Questions¶
Fix the 4 errors so the following code runs and returns the perimeter of a rectangle. For example,
recPerimeter(10, 20)
should return60
.Fix the 4 errors so the following code runs and returns the perimeter of a rectangle. For example,
recPerimeter(10, 20)
should return60
.-
Fix the 5 errors so the following code runs and returns the area of a square. For example,
squareArea(10)
should return100
. 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 return270
.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 return270
.-
Change the code below to create a function
tripCost
that calculates the cost of a trip. It should take themiles
,milesPerGallon
, andpricePerGallon
as parameters and should return the cost of the trip. For example,tripCost(100, 25, 2.24)
should return8.96
. Fix the errors on line 2 so the function
nameAndAge
returns the string “My name isnameString
and I amageInt
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 isnameString
and I amageInt
years old.” For example,nameAndAge("John", 18)
should return"My name is John and I am 18 years old."
-
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
Write a fruitful function
sumTo(n)
that returns the sum of all integer numbers up to and includingn
. For example,sumTo(10)
would compute 1 + 2 + 3 + … + 10 and return the value55
. Use this equation to find this sum: (n * (n + 1)) / 2. For example,sumTo(15)
should return120.0
.Write a fruitful function
sumTo(n)
that returns the sum of all integer numbers up to and includingn
. For example,sumTo(10)
would compute 1 + 2 + 3 + … + 10 and return the value55
. Use this equation to find this sum: (n * (n + 1)) / 2. For example,sumTo(15)
should return120.0
.-
Rewrite the function
sumTo(n)
that returns the sum of all integer numbers up to and includingn
. This time, print your answer before you return it. For example,sumTo(15)
should return120
. Write a function
areaOfCircle(r)
which returns the area of a circle of radiusr
. Make sure you import the math module in your solution to obtain an accurate value of pi. For example,areaOfCircle(31415.926535897932)
should return3100627668.0299816
.Write a function
areaOfCircle(r)
which returns the area of a circle of radiusr
. Make sure you import the math module in your solution to obtain an accurate value of pi. For example,areaOfCircle(31415.926535897932)
should return3100627668.0299816
.-
Finish the function to return the average of three numbers, but drop the lowest value. For example,
get_avg_drop_lowest(100, 10, 0)
returns55
andget_avg_drop_lowest(4, 3, 10)
returns7
. 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, return0
. Ifspeed
is between 61 and 80 inclusive, return1
. Ifspeed
is 81 or more, return2
. If it is your birthday, your speed can be 5 higher in all cases. For example,caught_speeding(60,False)
should return0
.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, return0
. Ifspeed
is between 61 and 80 inclusive, return1
. Ifspeed
is 81 or more, return2
. If it is your birthday, your speed can be 5 higher in all cases. For example,caught_speeding(60,False)
should return0
.-
Write the
check_guess
function below which computes if a guess is too low, too high, or correct. Return'too low'
ifguess
is less thantarget
,'correct'
if they are equal, and'too high'
ifguess
is greater thantarget
. For example,check_guess(5, 7)
returns'too low'
,check_guess(7, 7)
returns'correct'
, andcheck_guess(9, 7)
returns'too high'
. Write a function called
alarm_clock
that has parametersday
andvacation
. Given aday
of the week encoded as 0 = Sun, 1 = Mon, 2 = Tue, … 6 = Sat and a boolean indicating if we are onvacation
, return a string indicating when the alarm clock should ring. If we are onvacation
and it is a weekend (0 = Saturday or 6 = Sunday), it should return"off"
, and otherwise return"10:00"
. If we are not onvacation
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 parametersday
andvacation
. Given aday
of the week encoded as 0 = Sun, 1 = Mon, 2 = Tue, … 6 = Sat and a boolean indicating if we are onvacation
, return a string indicating when the alarm clock should ring. If we are onvacation
and it is a weekend (0 = Saturday or 6 = Sunday), it should return"off"
, and otherwise return"10:00"
. If we are not onvacation
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'
.