-1

I have a simple script here and i want to use the user input from my_function as argument for my otehrfunction:

def otherfunction(lname):
    print("This is the otherfunction. " + lname + " is your lastname")

def my_function():
    #b = "Black"
    b = input("Enter your name: ")
    otherfunction(b)

my_function()

when i use variable b and set it to b= "Black", the script does what it is supposed to do but when i say b = input(....) i get this error:

Enter your name: black
Traceback (most recent call last):
  File "function.py", line 44, in <module>
    my_function()
  File "function.py", line 41, in my_function
    b = input("Enter your name: ")
  File "<string>", line 1, in <module>
NameError: name 'black' is not defined
  • 3
    Your code runs fine on my machine - can't see any logical errors. How are you running Python? On a windows? Using IDLE? – JimmyCarlos Jun 20 '20 at 16:48
  • 2
    @JimmyCarlos OP is using Python 2. – timgeb Jun 20 '20 at 16:50
  • @timgeb you are correct i hada conda deactivated and run python 2.7- thanks dudes – bestethereumsites Jun 20 '20 at 16:51
  • Impressive detective work tim! How did you know it was Python 2? – JimmyCarlos Jun 20 '20 at 16:55
  • If you want to run this in python2, with `input` command, you would have to quote the input into string. Below is how you can do it. – Prashant Kumar Jun 20 '20 at 16:55
  • 2
    @JimmyCarlos the error states that it cannot recognize `black`. And this is not written in the code so it must be the user input. Now why would a user input be giving this error ?? Python could not identify the input means it was being treated as some variable or keyword or command. This would happen if it is not a string. This is how I could understan the issue. – Prashant Kumar Jun 20 '20 at 16:58
  • 1
    @JimmyCarlos well known duplicate. If you spend some time here sooner or later you'll know them all. – timgeb Jun 20 '20 at 17:33

3 Answers3

0

If timgeb is right in that you are running Python 2, the issue is with the input() command. On Python 2, it should be raw_input() instead.

See this link: Python 2.7 getting user input and manipulating as string without quotations

JimmyCarlos
  • 1,934
  • 1
  • 10
  • 24
0

If you are trying to run this from python console, and your input is black then it will give the above error in python2.7 .

While givin user input you will have to wrap the input into quotes.

So the input you give should be 'black'

    print("This is the otherfunction. " + lname + " is your lastname")

def my_function():
    #b = "Black"
    b = input("Enter your name: ")
    otherfunction(b)

my_function()

On running this, beow is how you should input and its corresponding output :

Enter your name: >> 'black'
This is the otherfunction. black is your lastname
Prashant Kumar
  • 2,057
  • 2
  • 9
  • 22
0

I runned your code and seems to work as it should, b is passed to lname as the argument and regardless b = “Black” in the line b = input b overwrites so it no longer is “Black”.

I noticed in the “name error” that Black is written as ‘black’. Black != black they are differente variables and since is on line 1 ‘black’ is not defined: check the spelling and capitalization and see it the problem fixes. Otherwise I have no clue what the problem is...

Hope it helps!