Computer Lab 03: functions, errors and exceptions

practical work

We use the code implemented last week, from Wentworth2012 and make it more flexible by encapsulating the core functionality in functions.

2.14.5 Create a function compute_final_amount that will have P,r,n,t as the input parameters. Comment the function appropriately. Make the code robust against dividing by zero runtime problem:

  1. Use if-condition to catch the case where the denominator is zero, print an error message (using 'print') and return from the function.
  2. Use if-condition to catch the case where the denominator is zero and raise an exception.
  3. Handle this exception completely with a “try-except” block, i. e., no if-condition needed.

Weekly homework 03

time difference variant of 2.14.7 and 2.14.8. Implement a function compute_time_diff(h1,m1,h2,m2) that returns two values - hours, minutes - time difference between t2-t1, where the time is expressed in hours and minutes. Expect 24 hours notation. Required file name: 03_weekly_hw.py.

Usage:

>>> h,m = compute_time_diff(13,20,15,10)
>>> print(h,m)
1 50

Graded homework

Solve homework 01 - Right triangle and submit it via Upload system in time! Check the deadline in Upload system.
  • Read chapters 6,7,8,9 from Wentworth2012. This material is for the next two lectures/labs.
courses/be5b33prg/labs/week_03.txt · Last modified: 2025/10/16 14:53 by sindlpa1