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
>>>
y = 10 a = 2
a+y = 12
12
y = 10 a = 2
>>>
Why
ReplyDeleteBecause at first y and a are global variable so we can not use y,a (variable) directly in function.
DeleteIf 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()
No proper answer for first and last sub division
ReplyDeleteBecause at first y and a are global variable so we can not use y,a (variable) directly in function.
DeleteIf 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()
Because at first y and a are global variable so we can not use y,a (variable) directly in function.
ReplyDeleteIf 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()
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.
Deleteso sorry i misunderstood a few things ... it turns out the other sources were wrong ( more reasons to stick to your website :)
DeleteCan't the variable a and y in the second branch be a local variable and carry on the process ?
ReplyDeleteI didn't understand the last part, what do you mean by
ReplyDeletein Function
myfunc()
Please read again.
DeleteHi
ReplyDeleteCan you upload a Video solution for this specific Question . It's difficult to understand .
Ok..
DeleteActually it's wrong the error I get when running this program is kindoff like
ReplyDeleteUnboundLocalError: local variable 'a' referenced before assignment
It means the local variable is assigned after the y
[Program finished]
what you want to say ???
Deletehe wants to say that he is getting an error when the program is being run
DeleteYes.
DeletePost a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )