Question

In: Computer Science

python 3 Post an example of a fruitful function, adding comments to explain what it does....

python 3

Post an example of a fruitful function, adding comments to explain what it does.
Then add an example of how to call the function and use its return value.

Remember, unless the goal of a function is to print, there should be no print statements.
A fruitful function uses return to give results back to main.
The function we name main, will take care of getting input, pass it to other functions, and those functions will return results to main, and main will then print those results.

Solutions

Expert Solution

Here is the code containing a useful function "mean" and the "main" function:

#this defines a function named mean
#which takes as argument an array names vals
#and calculates and returns the mean of the array
def mean(vals):
#iterate over values in the array
sumVals = 0
for val in vals:
sumVals += val
#divide by length to get average
return sumVals/len(vals)

#define main function
def main():
#take user input
n = int(input("Enter number of values: "))
vals = []
print("Enter values: ")
for i in range(n):
vals.append(int(input()))
print("Mean is", mean(vals))

#call main function
main()

Here is a screenshot of the code:

Here is a screenshot of a sample output:

Comment in case of any doubts.


Related Solutions

What is meant by the term sustainability and explain its 3 aspects? Include an example illustrating your comments.
                                                                                  The following is an extract from the latest sustainability report (2019) of the Woolworths Group: “It is through the power of partnerships that we will be able to amplify our impact and deliver on our bold ambitions to become...
What is an escape sequence? Give an example in Python, and describe what it does. What...
What is an escape sequence? Give an example in Python, and describe what it does. What Python function is used to determine the length of Python objects such as lists, tuples and string literals? For the string literal “Bjorggson”, show by Python code example how to obtain the fourth character of that string. Show by code example in Python how to concatenate two strings and assign the result to a variable. Declare a Python list named famfri with the first...
Explain the difference between maximizing profit and adding value using an example and explain why adding...
Explain the difference between maximizing profit and adding value using an example and explain why adding value is so critical to an organization.
Explain the difference between maximizing profit and adding value using an example and explain why adding...
Explain the difference between maximizing profit and adding value using an example and explain why adding value is so critical to an organization.
Need a paragraph and examples to answer the following: Decision Making: Post comments and explain, with...
Need a paragraph and examples to answer the following: Decision Making: Post comments and explain, with examples, how decision making and analysis, with the tools from our course, may be of value to you and your future endeavors. Include specific examples such as a company seeking to increase profits would .... Thank you, Below is one of the examples on how a student answers: This course has given me first hand experience with some of the common methods used for...
Python 3 can someone explain in very simple terms what EVERY line of code does, including...
Python 3 can someone explain in very simple terms what EVERY line of code does, including what j and i do def longest(string): start=0;end=1;i=0; while i<len(string): j=i+1 while j<len(string) and string[j]>string[j-1]: j+=1 if end-start<j-i: end=j start=i i=j; avg=0 for i in string[start:end]: avg+=int(i) print('The longest string in ascending order is',string[start:end]) print('The average is',avg/(end-start)) s=input('Enter a string ') longest(s)
Python 3 can you explain what every single line of code does in simple terms please...
Python 3 can you explain what every single line of code does in simple terms please so basically # every single line with an explanation thanks def longest(string): start = 0;end = 1;i = 0; while i<len(string): j = i+1 while j<len(string) and string[j]>string[j-1]: j+=1 if end-start<j-i: end = j start = i i = j; avg = 0 for i in string[start:end]: avg+=int(i) print('The longest string in ascending order is',string[start:end]) print('The average is',avg/(end-start)) s = input('Enter a string ')...
The post illustrates one example of the intersection of biography and history. What examples does Mills...
The post illustrates one example of the intersection of biography and history. What examples does Mills provide in his article "The Sociological Imagination" (posted right above this assignment)? Discuss another example from your own experience that illustrates this concept.
1. Implement the recursive LU factorization algorithm in Python. Use plenty of comments to explain your...
1. Implement the recursive LU factorization algorithm in Python. Use plenty of comments to explain your code. While you are coding, it is helpful to break up your code into sub-functions and test the sub-functions as you go along.
Implement the recursive LU factorization algorithm in Python. Use plenty of comments to explain your code....
Implement the recursive LU factorization algorithm in Python. Use plenty of comments to explain your code. While you are coding, it is helpful to break up your code into sub-functions and test the sub-functions as you go along.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT