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 [पृष्ठ ६२]
