Advertisements
Advertisements
प्रश्न
Predict the output of the given code snippet.
ptr=40
def result():
print ptr
ptr=90
def func(var):
if var<=60:
ptr=30
print ptr
result()
func(60)
func(70)लघु उत्तर
Advertisements
उत्तर
Trace:
- Inside
result(), the statementptr = 90makesptra local variable throughout that function. - Therefore,
print(ptr)tries to use the localptrbefore it has been assigned a value. - The program stops at
result(); neitherfunc(60)norfunc(70)executes.
UnboundLocalError: cannot access local variable 'ptr' where it is not associated with a value
To modify the global ptr, write global ptr inside result() before assigning to it.
shaalaa.com
या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
पाठ 3: Classes in Python - EXERCISE [पृष्ठ ६२]
