PYTHON create a program The program shall have two functions (methods): a main f
ID: 3777792 • Letter: P
Question
PYTHON create a program
The program shall have two functions (methods): a main function and a helper function. The main function shall do the following:
-declare an array that can hold 20 integer values
-use a loop to put the integer values 1, 2, 3, ..., 18, 19, 20 into the array
-pass the array to the helper function which will return an integer value
-print out the value returned by the helper function
The helper function shall do the following:
-traverse the array adding the odd values found in the array
-return the sum of the values.
The end result is that the program prints out the value 100.
Notes: Use local variables rather than global variables, if supported in the implementation language
Explanation / Answer
Python Program:
#helper function
def helper(array):
odd=0
for i in array: #traverse the array adding the odd values found in the array
if i%2!=0:
odd=odd+i
return(odd) #return the sum of the values.
def main():
array=[]
for i in range(1,21):
array.append(i)
x=helper(array) #pass the array to the helper function which will return an integer value
print(x) #print out the value returned by the helper function
if __name__ == "__main__":main()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.