Read pages 19-42 of chapter 2 of the book Blown To Bits.
3 * 4 + 5
then your answer would look like this:
3 * 4 + 5 12 + 5 17
import math def compute_period(length, gravity_accel): # computes period of simple pendulum given the cord length in meters # and the acceleration due to gravity in meters/sec/sec (e.g. 9.8) return 2 * math.pi * math.sqrt(length / gravity_accel)
compute_period(10, 9.8)
Will we get an integer result or a floating point result? Why?
compute_period(10)
Does the method use a default value for the acceleration due to gravity or does Python complain about this function call? Explain.
compute_period(9.8, 10)
Does Python report an error? Why or why not?
import math def compute_period(length, gravity_accel): # computes period of simple pendulum given the string length in meters # and the acceleration due to gravity in meters/sec/sec (e.g. 9.8) print(2 * math.pi * math.sqrt(length / gravity_accel))
What value is stored in the variable period if we execute the following instruction? Why?
period = compute_period(10, 9.8)
def mystery(n): value = 1 for i in range(1, n+1): value = value * 2 print(value)
mystery(7)
Show your work by tracing the loop as we did in class.
value = value * i
What does this revised method display if we call it as follows:
mystery(7)
Again, show your work by tracing the loop as we did in class.
def mystery(n): value = 1 for i in range(1,n+1): value = value * 2 return value
Store the function in a file, then load it in python3 and call it with different positive integers and observe the results. What do your observations suggest about how the return statement works?