226k views
3 votes
Write a Python program that has subprograms nested four deep and in which each nested subprogram references local variables, variables defined in all of its enclosing subprograms, and global variables.

1 Answer

1 vote

Answer:

Check the explanation

Step-by-step explanation:

CODE:

global_var = "global" #accessible to all functions

def level0():

level0_var = "level 0" #accessible to level 0,1,2,3

print();

print("Level 0 function:")

print("global variable: ",global_var)

print("local variable 0: ",level0_var)

def level1():

print();

print("Level 1 function:")

level1_var = "level 1" ##accessible to level 1,2,3

print("global variable: ",global_var)

print("local variable 0: ",level0_var)

print("local variable 1: ",level1_var)

def level2():

print();

print("Level 2 function:")

level2_var = "level 2" #accessible to level 2,3

print("global variable: ",global_var)

print("local variable 0: ",level0_var)

print("local variable 1: ",level1_var)

print("local variable 2: ",level2_var)

def level3():

print();

print("Level 3 function:")

level3_var = "level 3" #accessible to level 3

print("global variable: ",global_var)

print("local variable 0: ",level0_var)

print("local variable 1: ",level1_var)

print("local variable 2: ",level2_var)

print("local variable 3: ",level3_var)

level3()

level2()

level1()

level0()

Kindly check the attached image below to see the output (indentations are important):

Write a Python program that has subprograms nested four deep and in which each nested-example-1
Write a Python program that has subprograms nested four deep and in which each nested-example-2
User Tony Miller
by
4.3k points