Practice: Module 1 Project (Conditional Functions)#

1: Traffic light timer#

Two main considerations:

  1. Rush hour or not (if rush hour, then we use 6 second cycles, otherwise we use 9)

  2. Weekends or not (if weekends, then the rush hour rule doesn’t apply, and we just use 9)

 1def traffic_light_seconds(isRushHour, isWeekend):
 2    if isWeekend == False and isRushHour == True:
 3        return 6
 4    else:
 5        return 9
 6    
 7def traffic_light_seconds(isRushHour, isWeekend):
 8    if not isWeekend and isRushHour:
 9        return 6
10    else:
11        return 9
1a = 5
2print(a)
3a = a + 2
4print(a)
5
7
1# run
2rush = True
3weekend = True
4traffic_light_seconds(isRushHour=rush, isWeekend=weekend)
9
1# run
2r = True
3w = False
4traffic_light_seconds(isRushHour=r, isWeekend=bw)
6
1traffic_light_seconds(True, False)
6
  1. What are the names of the arguments in the function call?

  2. What are the names of the parameters?

  3. What are the value types of the parameters?

  4. What is the value type of the return value?

  5. What is the Boolean expression?

  6. What comparison operators are used?

  7. What Boolean/logical operators are used?

  8. What conditional pattern is being used here?

2: Shipping calculator#

Write a function called shipping_calculator that calculates the shipping cost based on the weight of the package and the destination. If the package is being shipped within the US and weighs less than or equal to 10 pounds, the shipping cost is \\(10. If the package is being shipped within the US and weighs more than 10 pounds, the shipping cost is \\\)20. If the package is being shipped internationally and weighs less than or equal to 10 pounds, the shipping cost is \\(20. If the package is being shipped internationally and weighs more than 10 pounds, the shipping cost is \\\)40.

 1# define v1
 2def shipping_calculator(destination, weight):
 3    # domestic, light
 4    if destination == "US" and weight <= 10:
 5        return 10
 6    # international, heavy
 7    elif destination != "US" and weight > 10:
 8        return 40
 9    # domestic heavy and international light
10    # i'm lazy to write out the Boolean expression :P
11    else:
12        return 20
13    
14# define v2
15def shipping_calculator(destination, weight):
16    # domestic, light
17    if destination == "US":
18        if weight <= 10:
19            return 10
20        else:
21            return 20
22    # international, heavy
23    else:
24        if weight <= 10:
25            return 20
26        else:
27            return 40
1# run
2d = "US"
3w = 5
4shipping_calculator(d, w)
10
  1. What are the names of the arguments in the function call?

  2. What are the names of the parameters?

  3. What are the value types of the parameters?

  4. What is the value type of the return value?

  5. What are the Boolean expressions?

  6. What comparison operators are used?

  7. What Boolean/logical operators are used?

  8. What conditional pattern is being used here?

3: Interest rate calculator for a mortgage loan.#

Three main considerations:

  1. loanLength: 15 starts at 7; 30 starts at 8

  2. creditScore (int) 0 to 850 - exceptional (800 and up) applies a 10% reduction to the interest rate (e.g., 7 would go down to 6.3); very good (740 to 799) applies a 5% reduction, and good (670-739) applies a 2.5% reduction

  3. isSecured: if True, then we get a flat 1 percentage point reduction of the interest rate after considering life of loan and credit score.

Returns an float that specifies the interest rate percentage as a function of the input params.

 1# define 
 2def interest_rate(loanLength, creditScore, isSecured):
 3    
 4    # loanLength consideration
 5    if loanLength <= 15:
 6        rate = 7
 7    else:
 8        rate = 8
 9    
10    # creditScore consideration
11    if creditScore >= 800:
12        rate = rate*0.9
13    elif creditScore >= 740:
14        rate = rate*0.95
15    elif creditScore >= 670:
16        rate = rate*0.975
17    
18    # isSecured consideration
19    if isSecured:
20        rate = rate-1
21    
22    return rate
1# run
2l = 15
3c = 700
4s = True
5interest_rate(l, c, s)
5.825

4: Travel caller for basketball#

is_traveling() returns True or False, depending on:

  1. stepCount: more than 2 = Travel (though subject to gather step and pivot considerations)

  2. gatherStep (True or False). If True, subtract 1 from step count before deciding if it’s a travel.

  3. isPivoting (True or False). If True, then call no travel, regardless of stepCount

 1# define
 2def is_traveling(stepCount, hasGatherStep, isPivoting):
 3    # if you're pivoting, nothing else matters, it's not a travel
 4    if isPivoting:
 5        return False
 6    # otherwise
 7    else:
 8        # if you're gather-stepping
 9        if hasGatherStep:
10            # we subtract 1 from your stepcount
11            # and check if it's greater than 2
12            return stepCount - 1 > 2
13        # otherwise
14        else:
15            # we just check if your stepcount is greater than 2
16            return stepCount > 2
1# run
2s = 4
3g = True
4p = False
5is_traveling(s, g, p)
True