Answer to Question #237367 in Python for SAUL

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.


1
Expert's answer
2021-09-15T00:10:33-0400
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!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS