# Review Questions

## Review Questions

#### User-defined functions

Q07.01 Write a function called `ft_to_in()`

which converts feet to inches. Note the conversion factor is 1 foot = 12 inches. Convert 6 feet into inches using your function.

Q07.02 Write a function called `m_to_ft()`

which converts meters to feet. Note the conversion factor is 1 meter = 3.28084 feet. Convert 5,000 meters into feet using your function.

Q07.03 Use the functions in questions Q07.01 and Q07.02 to convert 2 meters into inches.

Q07.04 Write a function that calculates the area of a circle based on a circle's radius. The formula for the area of a circle is A = \pi r^2 where A is area and r is radius. Use your function to calculate the area of circle with a radius of 5.

Q07.05 Write a function that converts degrees Celsius (C) to degrees Fahrenheit (F). The formula to convert between the two temperature scales is F = \frac{9}{5} C + 32. Convert 100 degrees C to degrees F using your function.

Q07.06 Write a function that converts Kelvin temperature (K) to degrees Celsius (C). The formula to convert between the two temperature scales is C = K - 273.15.

Q07.07 Use the functions in questions Q07.05 and Q07.06 to convert to convert the temperature at Standard Temperature and Pressure (STP) of 273.15 K into degrees F.

Q07.08 Use the functions in questions Q07.05 and Q07.06 to convert to convert the temperature at absolute zero, 0 K into degrees Celsius and degrees Fahrenheit.

Q07.09 Write a function called `hp_to_kw()`

which converts horse power (hp) into kilowatts (kW). Note the conversion factor is 1 hp = 0.7457 kW. Convert the horsepower of the average horse, 14.9 hp, into kilowatts (kW).

Q07.10 Write a function called `fun_logic()`

that accepts three boolean variables as its input (`a`

, `b`

, and `c`

). The output of `fun_logic()`

will be a single boolean variable that is only `True`

when either `a`

is `True`

, or `a`

, `b`

, and `c`

are all `False`

.

Q07.11 Write a function called `pn()`

that takes in a single number and outputs a string. If the input number is negative, output the string `'negative'`

. If the input number is positive, output `'positive'`

. Otherwise, output `'neither'`

. Test your function with a negative number, a positive number and `0`

.

#### Functions with multiple arguments

Q07.20 Write a function called `cyl_v()`

that calculates the volume V of a cylinder based on cylinder height h and cylinder radius r. The formula for the volume of a cylinder is below:

Use your function `cyl_v()`

to calculate the volume of a cylinder with height = 2.7 and radius = 0.73.

Q07.21 The universal gas law states that the pressure P, and temperature T of a volume V of gas with number of particles n is related by the equation below, where R is the universal gas constant.

Write a function called `gas_v()`

to calculate the volume V of a gas based on pressure P, temperature T, number of particles n and universal gas constant R. Use your function to find the volume of gas with the following parameters:

Q07.22 Most professional bakers weight their ingredients, while many home bakers use measurements like cups and tablespoons to measure out ingredients. Create a function that takes two arguments: ingredient and cups. The function will output the number of grams of the specified ingredient. For example, a skeleton outline of your function might look like:

```
def bake_conv(ingredient, cups)
<code>
return grams
```

Your function needs to accept the following ingredients: `'flour'`

and `'sugar'`

. The conversion factor for flour is 1 cup flour = 128 grams flour. The conversion factor for sugar is 1 cup sugar = 200 grams sugar. Use your function to convert a recipe that calls for 3 cups of flour and a quarter cup of sugar.

Q07.23 The gravitational force between two celestial bodies (planets, moons, stars etc) is calculated according to:

where F_g is the gravitational force, M is the mass of one of the celestial bodies, m is the mass of the other the celestial body, r is the distance between the two celestial bodies and G is the universal gravitational constant. G=6.667408 \times 10^{-11} m^3 kg^{-1}s^{-2}.

(a) Write a function called `grav_force()`

that accepts the mass of two celestial bodies and outputs the gravitational force produced.

Celestial Body | Mass | Distance from sun |
---|---|---|

Sun | 1.989\times10^{30} kg | 0 |

Earth | 5.98\times10^{24} kg | 149.6 \times 10^9 m |

Mars | 6.42\times10^{23} kg | 228 \times 10^9 m |

(b) Use your function `grav_force()`

and the table above to calculate the gravitational force between the earth and the sun.

(c) Use your function `grav_force()`

and the table above to calculate the gravitational force between the mars and the sun.

Q07.24 Write a function called `add3()`

that takes in 3 numbers and returns their sum. Only a single output variable (the sum of the three numbers) should be returned.

Test your function by writing code beneath the function definition that calls the function with input you create.

Q07.25 Write a function called `asq()`

that takes in two variables, `A`

and `B`

. Have `asq()`

output 3 values: the sum, the difference and the quotient of `A`

and `B`

as defined below:

`sum = A + B`

`difference = A - B`

`quotient = A/B`

Your function should accept two input arguments and output three values.

#### Functions with default arguments

Q07.30 (a) Rewrite the function in problem Q07.20 called `gas_v()`

with the default values n = 6.02 \times 10^{23}, R = 8.314 and P = 101,325.

(a) Use your modified function `gas_v()`

to calculate the volume of a gas at T = 500 K using the default arguments.

(b) Use your modified function `gas_v()`

to calculate the volume of a gas at T = 500K, under half the pressure p = 101,325/2.

Q07.31 In engineering mechanics, the tensile stress \sigma applied to a solid cylinder is equal to the tensile force on the cylinder F divided by the cylinder's cross sectional area A according to the formula below:

The standard diameter d of a cylinder pulled in tension in a tensile test using the ASTM D8 standard is d=0.506 inches.

Use the formula for stress \sigma and area A above to write a function called `stress()`

that calculates stress \sigma based on force F and diameter d. Use d=0.506 as the default diameter, but allow the user to specify a different diameter if they want.

Use your `stress()`

function to calculate the tensile stress \sigma in a cylinder with the default diameter and a tensile force F = 12,000.

Q07.32 One way to calculate how much an investment will be worth is to use the Future Value formula:

Where FV is the future value, I_0 is the initial investment, r is the yearly rate of return, and n is the number of years you plan to invest.

(a) Write a function called `future_value()`

which accepts an initial investment I_0 and a number of years n and calculates the future value FV. Include r=0.05 as the default yearly rate of return.

(b) Use your `future_value()`

function to calculate the future value of an initial investment of 2000 dollars over 30 years with the default yearly rate of return

(c) Use your `future_value()`

function to calculate the future value of the same initial investment of 2000 dollars over 30 years, but a rate of return of 8% (`0.08`

).

(d) Use your `future_value()`

function to determine when 2000 dollars is invested over 30 years, how much more do you make if the rate of return is 10% (`0.10`

) instead of 5% (`0.05`

).

Q07.33 Write a function called `s()`

that takes in 3 variables, `a`

, `b`

, and `c`

. Have your function `s()`

output the sum of `a`

, `b`

and `c`

. If no value is passed in for `c`

, set `c`

to the default value of `100`

.

#### Nested Functions

Q07.36 In mechanical engineering, there are a couple different units of *stress*. Units of stress include: Pascals (Pa), Mega Pascals (MPa), pounds per square inch (psi) and kilopounds per square inch (ksi).

(a) Write a function called `pa_to_mpa`

to convert between Pascals (Pa) and Mega Pascals (MPa). The conversion factor is 1 MPa = 10^6 Pa

(b) Write a function called `mpa_to_ksi`

to convert between Mega Pascals (MPa) and kilopounds per square inch (ksi). The conversion factor is 1 ksi = 6.89476 MPa

(c) Write a function called `ksi_to_psi`

to convert between kilopounds per square inch (ksi) and pounds per square inch (psi). The conversion factor is 1000 psi = 1 ksi

(d) Combine the three functions `pa_to_mpa`

, `mpa_to_ksi`

, `ksi_to_psi`

into a single function `pa_to_psi`

. Do this by calling the other functions as part of the `pa_to_psi`

function, not by rewriting the same code you wrote in parts (a), (b), and (c).

(e) Convert 2,500 Pa into psi using your `pa_to_psi`

function.

#### Functions in other files

Q07.40 Create a separate ** .py** file called

**. Inside of**

*greetings.py***include the code:**

*greetings.py*```
def hi():
print("Hi!")
```

Import your newly created ** greatings.py** file and run the function

`hi()`

.Q07.41 Create a separate ** .py** file called

**. Inside of**

*greetings.py***include the code:**

*greetings.py*```
def hello(name):
print("Hello " + name)
```

Import your newly created ** greatings.py** file and run the function

`hello()`

with your name as an input argument.Q07.42 Create a separate file ** .py** file called

**. Inside of**

*areas.py***include the code:**

*areas.py*```
def triangle(base,height):
area = 0.5*base*height
print("Triangle Area: ", area)
def rectangle(length, width):
area = length* width
print("Rectangle Area: ", area)
```

Import your newly created ** areas.py** file and run the functions

`triangle()`

and `rectangle()`

with the same two input arguments: `2`

and `3`

.#### Errors, Explanations, and Solutions

For the sections of code below, run the lines of code. Then explain the error in your own words. Below your error explanation, rewrite and run an improved section of code that fixes the error.

Q07.80 Run the code below and explain the error. Rewrite the code and run it error free.

```
def add_me(num)
return num + 2
add_me(1)
```

Q07.81 Run the code below and explain the error. Rewrite the code and run it error free.

```
def add_you[num]:
return num + 2
add_you(2)
```

Q07.82 Run the code below and explain the error. Rewrite the code and run it error free.

```
def my_func():
print('yup')
my_func('yup')
```

Q07.83 Run the code below and explain the error. Rewrite the code and run it error free.

```
def nothing():
nothing()
```

Q07.84 Run the code below and explain the error. Rewrite the code and run it error free.

```
def plus(2+2):
return 4
nothing()
```

Q07.85 Run the code below and explain the error. Rewrite the code and run it error free.

```
def first_a(a):
return a[0]
first_a(1)
```