saving . . . saved why an error occur in the below program code has been deleted. why an error occur in the below program code has been hidden .
why an error occur in the below program code
Title
Question
a=10
b=20
print(id(a))
def func(x,y):
    y=a  
    a=2
    print(y)
    return x+y
n1=int(input("Enter no"))
n2=int(input("Enter"))
print(func(n1,n2))

Python-3.4.3 Getting-Started-with-Functions 02-03 min 40-50 sec 22-06-19, 5:29 p.m. Cinil

Answers:

It seems that the function part is not indented properly.
24-06-19, 11:11 p.m. ary123in92
if the indentation is correct also then error will be displayed. the proper intended program is as follows
a=10
b=20
print(id(a))
def func(x,y):
     y=a
     a=2
     print(y)
     return x+y
n1=int(input("Enter no"))
n2=int(input("Enter"))
print(func(n1,n2))
25-06-19, 10:33 a.m. Cinil

Login to add comment


This is because you are using a global variable inside a function. You are using 'a' in func(x, y) but 'a' is global.
To use a global variable inside a function add 'global a' inside a function.
04-05-20, 9:31 p.m. pratikbpingale9075@gmail.com


Log-in to answer to this question.