0

I have defined a simple function in Python that takes 'Strings' as an argument and tried to manipulate the code a bit, and saw a difference while calling the function with those two codes.

This is the first code:

def my_name(first_name,last_name):
    first_name = input('enter the first name:')
    last_name = input('enter the last name:')
    print(first_name+' ' + last_name)

my_name('first_name','last_name')

In this program, I need to use quotation marks with the argument and if I don't use them I get an error. Why is this so?

But for the second program, I don't need to use quotation mark if I still use them, I don't get any find of error but for the previous program I have to use quotation otherwise it results in an error.

Please help. Here are snippets of my code: First code: enter image description here

Getting error while running without quotation enter image description here

Second code: enter image description here

Thanks in advance!!

4
  • In the first code you are passing 2 strings as a parameter to the my_name function, if you remove the lines that have the input() it will print first_name last_name and when you remove the quotation you are passing as a parameter 2 variables that are not have been initialized and need to receive values (which happens in the second code). Commented Jun 23, 2022 at 17:24
  • But, when we store string in any variable then we don't need to put the quotation mark around the variable name while running the code, python just consider it as a variable so, why this time a variable needs to be enclosed in a quotation mark Commented Jun 23, 2022 at 18:22
  • Do not use images for code or tracebacks or output. Copy and paste the text into the question. Format tracebacks and output (usually) as code. Commented Jun 23, 2022 at 18:29
  • Ok sure, I didn't know about it actually. Thanks for the feedback. Commented Jun 24, 2022 at 15:21

1 Answer 1

1

Given def f(arg): pass, the call f(s) fails with NameError: name 's' is not defined because the name s in the call is not defined. If you add s = 'some string' before the call, then the call works because s is defined. This and related matters is explained in the Python Tutorial

Sign up to request clarification or add additional context in comments.

5 Comments

Why is this so, I am asking a user to type his or her name in the first_name and last_name variables so, how come these two variables are not defined?
Is this because as I used this inside the fuction so, its scope becomes limited? Can any please provide the reasoning behind it as why I need to use enclose the variable inside quotation during the fuction call.
In any function, the code within the def is not executed until the function is called, and the scope of names bound within a function is normally limited to the function. So first_name is not defined when calling the function.
Also note that first_name=input... within the function throws away any value passed as the first_name argument.
Thanks for the explaination, but accept my apologies as I am unable to understand what you are tyring to say so, can you please explain the same to me by providing some kind of example. That would be a great help if you could so.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.