Question #237367

Create a function with a local variable. Show what happens when you try to use that variable outside the function. Explain the results.


Expert's answer

def func():
    name="John" #local variable
    print("Hello "+name)
func()


print(name) #this line causes an error since the variable name is defined locally in the function func 
#and therefore it cannot be used outside the function

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS