# 5.16. 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`.

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

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`.

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."`

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`.

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`.

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`.

12. 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'`.

You have attempted of activities on this page