76.8k views
1 vote
Find solutions for your homework

Find solutions for your homework

engineeringcomputer sciencecomputer science questions and answershow can i get this function to print properly and say i want a=2 and b=2 def get_total(a, b): #enclosed variable declared inside a function total = a + b def double_it(): #local variable double = total * 2 print(double) double_it() #double variable will not be accessible print(double)
Question: How Can I Get This Function To Print Properly And Say I Want A=2 And B=2 Def Get_total(A, B): #Enclosed Variable Declared Inside A Function Total = A + B Def Double_it(): #Local Variable Double = Total * 2 Print(Double) Double_it() #Double Variable Will Not Be Accessible Print(Double)
how can i get this function to print properly and say i want a=2 and b=2

def get_total(a, b):

#enclosed variable declared inside a function

total = a + b

def double_it():

#local variable

double = total * 2

print(double)

double_it()

#double variable will not be accessible

print(double)

return total

User Huzzah
by
7.7k points

1 Answer

4 votes

def get_total(a, b):

# Enclosed variable declared inside a function

total = a + b

def double_it():

# Local variable

nonlocal total # Use the 'nonlocal' keyword to modify the enclosed variable

double = total * 2

print(double)

double_it()

# The double variable will now be accessible

print(total) # Print the total value outside the double_it() function

return total

get_total(2, 2)

User Rob Hruska
by
8.6k points