This is the solution to Homework 3: Problems - Python debugging and string manipulation.

The following figure illustrates the grade distribution for this homework.

This homework aims at giving you some experience with String manipulation in Python and debugging a simple Python script. String manipulation is one of the greatest strengths and reasons for popularity of Python. For both questions below, submit your Python scripts in the corresponding *.py files, and add a readme.md file in HW 3 folder of your project if you need to add any additional explanation (Don’t forget to use markdown syntax highlight in your readme file).

1.  Download this Python script. This Python script is full of syntax, runtime, and semantic errors. Please identify and corrent these errors, such that code becomes fully executable and outputs the following message on the screen, when run 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 the 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 an 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 math module.
Therefore, if using math module, then,

sqrt(25) = 5.0000,

which is obviously 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:

• You need to get familiar with string formatting in print function. See Lecture 4.

• Note that in Python, the interpreter is sensitive to indentations in your codes. All lines must be aligned at the beginning (except for looping and branching, … which we will get to them later on).

• Become familiar with escape characters in Python, such as '\n', '\t', and '\t'.

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.

2.  Write a Python script that is directly executable from the Git Bash command line, without any need to invoke python interpreter. The script should produce exactly the following output, including line breaks and separations. Each part of the output (separated by a line-break, or a new-line character '\n' from the rest) is generated by only and only one call to print function.

This is Python version 3.5.2

Python is the best language for String manipulation!

!noitalupinam gnirtS rof egaugnal tseb eht si nohtyP

!otlpnmgit o gunlte h inhy

pYTHON IS THE BEST LANGUAGE FOR sTRING MANIPULATION!

The sentence 'Python is the best language for String manipulation!' contains
4 'a' letters, and
0 'A' letters!

Python
is
the
best
language
for
String
manipulation!

PYTHON
IS
THE
BEST
LANGUAGE
FOR
STRING
MANIPULATION!


Here are some hints (all the information you need to answer this question can be found in Lecture 4):

• To make the script executable, add the appropriate shebang to the script’s file.

• Import the appropriate module to get the python version command, as we did in class. Use print function to print the Python version.

• Define a String variable my_string which contains the string value “Python is the best language for String manipulation!” ( Of course, you can totally accomplish this question without defining a string variable, by only using the string value itself wherever needed… This is also totally fine at this point in our course, if your prefer this method).

• Then use the appropriate examples and external links provided in Lecture 4, to find the proper string methods that can manipulate this string variable/value in order to generate each of the outputs printed above.

• Again, note that for each output, the entire output must result from one single call to function print.

• Wherever needed, use the escape character value '\n' in print input arguments, in order to generate a line-break (or new line) in the output.