# Review Questions

## Review Questions

#### User-defined functions

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

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

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

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

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

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

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

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

Q05.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).

#### Functions with multiple arguments

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

Q05.21 The universal gas law states that the pressure P, and temperature T of 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:

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

Q05.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 bodies, 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.

#### Functions with default arguments

Q05.30 (a) Rewrite the function in problem Q05.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.

Q05.32 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.

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

).

#### Nested Functions

Q05.32 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 with 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

Q05.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()`

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

Q05.80 Run the code below and explain the error. Write the code and run it error free.

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

Q05.81 Run the code below and explain the error. Re-write the code and run it error free.

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

Q05.82 Run the code below and explain the error. Re-write the code and run it error free.

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

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

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

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

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

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