Please tell me what is wrong and why it is wrong? Error when input() is given below def(): When I removed arguments there is no error. Please tell me what is wrong with the code that has error. Thanks
No error when input given below
CodePudding user response:
it's because you are trying to pass arguments to the function which aren't initiated yet.
as python program starts executing it notes down the function name and jump directly to next line after end of function body.
so when you try to call function name you tried to pass it three arguments or variables which aren't defined yet if you move input statements out of the function body they gets executed and initiate them.
CodePudding user response:
You define the function details
so that it expects three parameters as input. That seems unnecessary in what you're doing, since you actually define the variables inside the function.
Those variables don't exist outside the function, so when you call details(name,age,address)
, you get a NameError, because a variable called name
doesn't exist in the scope where you are.
Try removing the three parameters from the function and just make it details()
.
CodePudding user response:
This is due to scope visibility of you function. If you consider the snippet that gives you error: "name", "age" and "address" are variable that exists inside the scope of the function "details" so you can't refers to these variable outside the scope and this cause the error. In the other hand, you define these variable outside the function and in the same scope of "detail" function so you can use it and no error is given.