Problem

Python

Download this Python script. This Python script is full of syntax, runtime, and semantic errors. Please identify and correct these errors, such that code becomes fully executable and outputs the following message on the screen when running from the Git Bash command line:

The life expectancy for the millennials is projected to be 120 years! (But don't believe it...)


    A recent study published in the journal of Nature, discovered that over the past century,
    although life expectancy has significantly increased due to technological advances,
    the maximum life span of the oldest people in the world has not changed much.


Cardano was the first to introduce complex numbers of the form a + sqrt(-b) into algebra, but he had misgivings about it.
In his solution to an algebra equation, he encountered the solution 5 + sqrt(-15) for the unknown, which is now mathematically represented by

       (5+3.872983346207417j)

in Python, which can also be obtained as the addition of real and imaginary numbers in Python like this


      5 + sqrt(-15) = (5+3.872983346207417j),


      which can also be manually stated as


      (5+3.872983346207417j)





One final note:

        In python the sqrt function from math and cmath modules are different.
        The sqrt function that returns "float" results is sqrt from the math module.
        Therefore, if using the math module, then,

                sqrt(25) = 5.0000,

        which is a float (real number).




Also note that by convention, 0**0 = 1 in Python.
And division by 0, will give you a runtime exception: 1/0 = NaN

Identify at least one example in the code for each of the error types.

Here are a few hints:

Solution

Python

The corrected file can be downloaded from here. Examples of different types of programming errors:

  1. semantic error example:

    • line 15, in the wrong script: The purpose is to print the complex number 5+sqrt(-15), but instead, 5-15j is printed by the wrong script.

    • line 35, in the wrong script: The purpose is to print float number, but since we used sqrt from cmath module, the output is in complex format, which is not what we intend to obtain and print.

  2. runtime error example:

    • in line 45, in the wrong script, division by zero, is a runtime exception.

  3. syntax error example:
    Almost any other error in the code is a syntax error. For example, lines 11,12,13 should be aligned, but they are not.

Comments