Skip to main content

Midterm Review (Keith’s sections only!)

Try solving these in Thonny. These are similar style & level to the “long” questions on a midterm test.

Tracing

Given the Python code below, fill in all the blanks assuming the user typed 12 for the input. Feel free to use a (non-programmable) calculator!

# initialize defaults
total = 4

# user types 12
num = int(input("What's a cool number? ")) # num's value is now: _______

# do math
num = num + 2 # num's value is now: _______
num = num + 4 # num's value is now: _______
num = num + 6 # num's value is now: _______
if num > 25:
    num = num + 3
else:
    num = num / 2
# num's value is now: _____    
total = total + (num % 5)

# output result
print(total) # the printed total is: ______

Approximation of ln(1+x)

Fall semester students can skip this one unless they already understand series or want to learn a bit of future-math.

 
If you’re not familiar with Summation Notation / Sigma / Series, then check out this video
You’ll need this in future math courses if you haven’t learned it yet!  
 

We can approximate the natural logarithm of 1+x using the Taylor series:
\[ \ln(1 + x) = \sum_{n=1}^{N} (-1)^{n+1} \frac{x^n}{n}, \quad |x| < 1 \]

The larger the value N is, the more precise the approximation will be. Using N=200, write Python code to print the approximate value of ln(1 + 0.873).

Hint: Use a for loop and a variable to hold the sum of all the calculations.

Delivery Driver

You have a full-time job in the summer delivering packages for a small local company called Nozama.

You are paid based on how far you drive each day making the deliveries. For every whole kilometer (km) you drive, you’ll be paid $1.60.

However, if you drive more than 100km in one day then Nozama deducts a $15.00 maintenance fee for that day from your pay. (Try to find some more efficient routes next time!)

On your first day of work you only drove 50km (it’s ok, you’re new at this job). Each day after that you got a bit faster and drove 10km more than the previous day.

Write a Python function to calculate and print how much you will get paid for your first 7 days of work. There are no parameters needed for this function and it will not return any value.

For each day that you worked print a message like this with the day number, kilometers driven, and the pay you earned. For both of these dollar amounts do not do anything to round them or control the number of decimal places.

Day 1: 50km = $80.0

At the end, print a message with your total pay like this:

Total pay = $123.45

You must use a for loop in your solution.

Parking Cost

The city of Montréal charges for street parking by the hour. The first 2 hours cost $3.50/hour. Hours 3 and 4 cost $5.00/hour (peak pricing). Any hour beyond 4 hours costs $7.25/hour (you really shouldn’t be parked that long!). Parking is capped at a maximum of 8 hours.

Write a function called parking_cost that accepts a whole number of hours parked (between 1 and 8 inclusive). You can assume the input is always valid.

For each hour a car is parked, print the cost for that hour and the running total (i.e. the total cost up to and including that hour) like the following (this example is for 3 hours):

Hour 1: $3.50 - Total: $3.50
Hour 2: $3.50 - Total: $7.00
Hour 3: $5.00 - Total: $12.00

You must use a for loop in your solution.

Workout Calories

Your friend just joined a gym and wants to track how many calories they burn over a 6-day workout week (they rest on day 7).

On day 1 they burn a certain number of calories. Each subsequent day they push a little harder and burn 15% more calories than the previous day (round up to a whole number). However, if on any day they would burn more than 300 calories, they stop it at exactly 300 (doctor’s orders!).

Write a function called calories_calculator that: - Accepts the number of calories burned on day 1 as a parameter. - Uses a for loop over the 6 workout days. - For each day, prints whether the cap was applied or not, the calories burned, and the running weekly total:

Day 1: 200 cal burned — Weekly total: 200 cal
Day 2: 230 cal burned — Weekly total: 430 cal
Day 3: 265 cal burned — Weekly total: 695 cal 
...
Day 6: 300 cal burned (capped) - Weekly total: ...

Week complete! Total calories burned: ...
Average per day: ... cal

Baggage Fees at FlyHigh Airlines

FlyHigh Airlines charges baggage fees based on how much a checked bag weighs over the free allowance of 23kg.

The fee applies to the entire weight over the limit.

Excess weight Rate
0 or under $0
1-5kg over $10.00/kg
6-15kg over $14.00/kg
16kg or more over $20.00/kg

If the flight is an international flight then all fees are multiplied by 1.5

For example, a bag weighing 30kg on a domestic flight would cost:
7 × $14.00 = $98.00
because 30kg − 23kg = 7kg over, and 7 is in the range of 6–15 in the table above.

Write a function called baggage_fee that has parameters for: - the weight of the bag (in kg)
- whether the flight is international or not
and returns the fee.

This is an example of how your function would be called:

weight: float = 30.0  
fee: float = baggage_fee(weight, False)