Computer Science, asked by ranuj, 4 months ago

Predict the output of following code :
a = 10
y = 5
def myfunc():
y=a
a = 2
print ('y=", y, "a=", a)
print ("a+y=", a+y)
return a+y
print { "y=", y, "a=", a )
print myfunc()
print ("y=", y , "a=", a)​

Answers

Answered by dreamrob
6

Given code :

a = 10

y = 5

def myfunc():

   y = a

   a = 2

   print ("y =", y, "a =", a)

   print ("a+y =", a+y)

   return a+y

print ( "y =", y, "a =", a )

myfunc()

print ("y =", y , "a =", a)

Output :

y = 5 a = 10

Traceback (most recent call last):

    File "main.py", line 10, in <module>

         myfunc()

    File "main.py", line 4, in myfunc

         y = a

UnboundLocalError: local variable 'a' referenced before assignment

After making some changes in the code :

a = 10

y = 5

def myfunc():

   global a

   y = a

   a = 2

   print ("y =", y, "a =", a)

   print ("a+y =", a+y)

   return a+y

print ( "y =", y, "a =", a )

myfunc()

print ("y =", y , "a =", a)

Output :

y = 5 a = 10

y = 10 a = 2

a+y = 12

y = 5 a = 2

Attachments:
Similar questions