In: Computer Science
Write a python program that has subprograms (functions) nested 3 deep. Each subprogram should reference variables defined in each of its enclosing subprograms. To submit this program, copy/paste the program and a screenshot of your output when you run it. Use comments liberally so that we can understand your work! You can run JavaScript wherever you prefer, but it is available on repl.it.
ANSWER - Python program pasted below.
#Function definition for finding the sum of all items in a
list
#outer function1 - level 1
def function1(list1):
#outer function2 - level 2
def function2():
#The variable list1 is passed from outer function1 to
function2
#sum is initialized to 0
sum=0
#taking each item from the list and add it with the variable
sum
for item in list1:
sum=sum+item
#inner function - level 3
def function3():
#variable sum is passed from outer function2 to inner
function
print("Sum of all items in list ",list1,"is ",sum)
#function 3 is called
function3()
#function 2 is called
function2()
#main program
#reading a list of numbers
numbers=input("Enter a list of numbers(space separated):")
#Split them and convert it to an integer list
list1=list(map(int,numbers.split()))
#call the outer function
function1(list1)
Python Code in IDLE pasted below for better understanding of the indent.
Output Screen