Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How can I code this in python? Part One Using a main function, Docstrings Write a function called hello. The function has no arguments and

How can I code this in python?

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 hellos 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]

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Object Oriented Databases Prentice Hall International Series In Computer Science

Authors: John G. Hughes

1st Edition

0136298745, 978-0136298748

More Books

Students also viewed these Databases questions

Question

=+Are they specific or general in nature?

Answered: 1 week ago

Question

=+ What is the nature of the contracts or agreements with unions?

Answered: 1 week ago

Question

=+What is the procedure for labor relations in the workplace?

Answered: 1 week ago