CIS 41A Unit F, Exercise F

This assignment consists of one script with three parts.

At the top of your script, put the following multi-line comment with your information:

'''
Your name as registered, with optional nickname in parentheses
CIS 41A   Fall 2021
Unit F, Exercise F
'''

All print output should include descriptions as shown in the example output below.

Part One – Using a main function, Docstrings

Write a function called hello. The function has no arguments and no return value. It simply prints the text "Hello World". Include a docstring that describes the function.

Write a main function, as described by the Python main function reading.

Call main, as described by the Python main function reading.

From main, call hello and then print hello’s docstring.

Part Two – Error Handling

Write a function called printListElement. The function has two arguments and no return value. The first argument is a list, and the second argument is a list index. The function will print an element from the list as determined by the list index. If the list index is invalid, print an error message.

We could accomplish this with a logic test, but instead, we will manage this with error handling.

Write a try block that attempts to print the list element. Catch any errors with an except block, print an error message.

From main, create a myList list with elements 0, 1, 2 by using the list and range commands.

Then, call printListElement with your list and a list index value of 3.

Part Three – How Python function arguments are treated

There can be some confusion as to how Python functions treat their arguments - is it by reference or by value? Explore this for yourself.

From main, create a myInt variable and give it the value 3. Also create a myList list with elements 0, 1, 2.

Print the IDs of myInt and myList. Also print the ID of the last element of myList.

Now create a function called byVal which has one argument. In the function, add 7 to the argument. Print the ID of the argument before and after the change.

Create a second function called byRef which has one argument. In the function, add 7 to the last element in the list. Print the ID of the argument and the ID of the last element of the argument before and after the change.

Now call byVal with myInt and then call byRef with myList. Next, again print the IDs of MyInt, myList, and the last element of myList. Finally, print myInt and MyList from main. Can you explain the results?

Here are two sites that do a good job of explaining how Python functions work:
Is Python call by reference or call by value
Parameters and Arguments

Sample Execution Results:

Hello world
Help on function hello in module __main__:

hello()
    This function prints Hello World

Error: bad index number.

Original ID of myInt in main is 1406102608
Original ID of myList in main is 2884791210120
Original ID of myList's last element in main is 1406102576
Original ID of parameter in byVal 1406102608
ID of parameter in byVal after change 1406102832
Original ID of parameter in byRef 2884791210120
Original ID of parameter's last element in byRef 1406102576
ID of parameter in byRef after change 2884791210120
ID of parameter's last element in byRef after change 1406102800
ID of myInt in main after call to byVal is 1406102608
ID of myList in main after call to byRef is 2884791210120
ID of myList's last element in main after call to byRef is 1406102800
myInt is now: 3
myList is now: [0, 1, 9]

Add the following at the end of the script to show your results:

'''
Execution results:
paste execution results here
'''

Submit your finished py script in Canvas, Exercise F.