Q. Predict the output of the 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)

Answer =

Output:-

y = 5 a = 10

Error

Explanation :- Because at first y and a are global variables so we can not use y,a (variable) directly in function.
If we want to use "y" and "a" in function, we have to write
global y
global a
before  

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

in myfunc() Function.
So, the Correct program is :-
a = 10
y = 5
def myfunc():
    global y,a
    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)

Output :-

y = 5 a = 10
y = 10 a = 2
a+y = 12
12
y = 10 a = 2

>>> 

16 Comments

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

  1. Replies
    1. Because at first y and a are global variable so we can not use y,a (variable) directly in function.
      If we want to use "y" and "a" in function, we have to write
      global y
      global a
      before

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

      in Function
      myfunc()

      Delete
  2. No proper answer for first and last sub division

    ReplyDelete
    Replies
    1. Because at first y and a are global variable so we can not use y,a (variable) directly in function.
      If we want to use "y" and "a" in function, we have to write
      global y
      global a
      before

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

      in Function
      myfunc()

      Delete
  3. Because at first y and a are global variable so we can not use y,a (variable) directly in function.
    If we want to use "y" and "a" in function, we have to write
    global y
    global a
    before

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

    in Function
    myfunc()

    ReplyDelete
    Replies
    1. but if they are global variables they can be accessed throughout the program (both inside and outside functions) . global keyword is used to change local variable (defined inside a function) to a global variable. please check your output again ... i checked other sources and the output is not this.

      Delete
    2. so sorry i misunderstood a few things ... it turns out the other sources were wrong ( more reasons to stick to your website :)

      Delete
  4. Can't the variable a and y in the second branch be a local variable and carry on the process ?

    ReplyDelete
  5. I didn't understand the last part, what do you mean by

    in Function
    myfunc()

    ReplyDelete
  6. Hi
    Can you upload a Video solution for this specific Question . It's difficult to understand .

    ReplyDelete
  7. Actually it's wrong the error I get when running this program is kindoff like
    UnboundLocalError: local variable 'a' referenced before assignment
    It means the local variable is assigned after the y

    [Program finished]

    ReplyDelete
    Replies
    1. he wants to say that he is getting an error when the program is being run

      Delete

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post