Question
USE PYTHON!!!!!!!!! Part One Using a main function, Docstrings Write a function called hello. The function has no arguments andno return value. It simply
USE PYTHON!!!!!!!!!
Part One – Using a main function, Docstrings
Write a function called hello. The function has no arguments andno return value. It simply prints the text "Hello World". Include adocstring that describes the function.
Write a main function, as described by the Python main functionreading.
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 twoarguments and no return value. The first argument is a list, andthe second argument is a list index. The function will print anelement from the list as determined by the list index. If the listindex is invalid, print an error message.
We could accomplish this with a logic test, but instead, we willmanage this with error handling.
Write a try block that attempts to print the list element. Catchany errors with an except block, print an error message.
From main, create a myList list with elements 0, 1, 2 by usingthe list and range commands.
Then, call printListElement with your list and a list indexvalue of 3.
Part Three – How Python function arguments are treated
There can be some confusion as to how Python functions treattheir arguments - is it by reference or by value? Explore this foryourself.
From main, create a myInt variable and give it the value 3. Alsocreate a myList list with elements 0, 1, 2.
Print the IDs of myInt and myList. Also print the ID of the lastelement of myList.
Now create a function called byVal which has one argument. Inthe function, add 7 to the argument. Print the ID of the argumentbefore and after the change.
Create a second function called byRef which has one argument. Inthe function, add 7 to the last element in the list. Print the IDof the argument and the ID of the last element of the argumentbefore 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 ofmyList. Finally, print myInt and MyList from main. Can you explainthe results?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started