This question already has answers here:
input() error - NameError: name '...' is not defined
(15 answers)
Closed 4 years ago.
I have recently started learning to work in bash/unix... I'm still very new.. not really sure what its even called.
I am experienced with python. Having worked with the language the past 4 years for network engineering and data analysis.
Now we are doing this virtual environment thing, and I'm having some trouble wrapping my head around it.
Currently I have the following code in a file called helloWorld.py stored in the current working directory.
#! /usr/bin/env python
def hello():
name = str(input('\n\nHello, what is your name? \n'))
print('\nHello World')
print('But more importantly... \nHello ' + name)
return
hello()
So. my issue is. when I run the code in the shell, I get the following:
[currentDirectory]$ python helloWorld.py
Hello, what is your name?
randomname <-- typed by user.
Traceback (most recent call last):
File "helloWorld.py", line 8, in <module>
hello()
File "helloWorld.py", line 3, in hello
name = str(input('\n\nHello, what is your name? \n'))
File "<string>", line 1, in <module>
NameError: name 'randomname' is not defined
It seems to not be recognizing that the variable is a string.
Code works fine in IDE outside of bash shell. Pretty basic code.
But this virtual enviroment linux/unix/shell/bash stuff is super new.
Like this is literally day 1. I have been able to create and save a file and change directories. This was my first test of writing python in the shell and I immediately hit a roadblock.
Sorry for the probably super easy question.
Thanks for any help.
BTW:
this DOES WORK IF the user puts quotes around what they type. But that defeats the purpose of using the str() converter around the input line in the function.
How can I make it so user can just type whatever?
In Python 2, raw_input() returns a string, and input() tries to run
the input as a Python expression.
Try this:
#! /usr/bin/env python
def hello():
name = raw_input('\n\nHello, what is your name? \n')
print('\nHello World')
print('But more importantly... \nHello ' + name)
return
hello()
Related
This question already has answers here:
Why is Python running my module when I import it, and how do I stop it?
(12 answers)
What does if __name__ == "__main__": do?
(45 answers)
Closed 10 months ago.
Please I am pretty new to python, I am trying to import a function from a python file A to another python file B. But as soon as I import the file or just the function of the python file A, the entire imported file runs as soon as i run python file B.
# pythonfile A
def email():
print('Hello how are you doing?')
email()
user = input('Enter response')
print(user)
Running this code I get:
Hello how are you doing?
Enter your response I am good
I am good
# pythonfile B
import pythonfileA
pythonfileA.email()
Running this code I expected to get:
Hello how are you doing?
but I get
Hello how are you doing?
Enter your response I am good
I am good
instead. I also tried another alternative:
# pythonfile B
from pythonfileA import email
email()
Running that code I expected to get:
Hello how are you doing?
but I get
Hello how are you doing?
Enter your response I am good
I am good
Please help me, been searching the internet for solutions but to no good.
This question already has answers here:
input() error - NameError: name '...' is not defined
(15 answers)
Closed 3 years ago.
I had a problem while coding I tried to solve it by abstracting the non-problematic parts and making a new Pycharm project with only the problematic part so that I can see it more clearly but I still can't see any problems. I think it may be because of a software error but I am not sure
Here is the problematic part:
import sys
import os
User_Name=str(input("What is your name?"))
print (User_Name)
Here is the outcome:
What is your name? #X Traceback (most recent call last): File
"C:/Users/USER/PycharmProjects/More_Complex_Projects/BLINDFOLDED.py",
line 4, in
User_Name=str(input("What is your name?")) File "", line 1, in NameError: name 'X' is not defined
Process finished with exit code 1
I expected it to print out the variable User_Name but it just gives an Name error
Seems like you're using Python 2. In this case, you should use raw_input, which doesn't attempt to parse the inputted string and not input:
user_name = raw_input("What is your name?")
# Here -----^
After a bit of research :
for python 2.7 https://docs.python.org/2.7/library/functions.html#input
for python 3.X https://docs.python.org/3/library/functions.html#input
Now what you have is 2.7 , therefore you should tend to use raw_input() rather than the input().
Try :
import sys
import os
User_Name=str(raw_input("What is your name?"))
print (User_Name)
I'm using SublimeText 3. I installed SublimeREPL to run my current Python file so I could see returns on inputs on simple practice exercises. Here's my ridiculously simple code:
name = input("What is your name: ")
print name
I went to test it out and I am getting the following error:
What is your name: Justin
Traceback (most recent call last):
File "Practice Exercise 1.py", line 2, in <module>
name = input("What is your name: ")
File "<string>", line 1, in <module>
NameError: name 'Justin' is not defined
***Repl Closed***
Seems like it wants to think of "Justin" as a function or something. If you guys could help me out, that'd be great.
You need your input to be in quotes... i.e. What is your name: "Justin"
See here as an example https://www.python-course.eu/input.php
Seems like you are running Python2. In which case, change input() to raw_input().
If you're using python 3, print is a function and must be called using parenthesis.
print(name)
Seems you’re using python 2. You should probably be using python 3 unless you have a reason (eg: backwards compatibility). In python 2, input() interprets the input so you probably want to use raw_input() like this:
name = raw_input("What is your name: ")
print name
This question already has answers here:
error in python d not defined. [duplicate]
(3 answers)
Closed 8 years ago.
I have a super basic question, but I'm just starting to learn python. My script:
print('What is your name?')
person = input("Enter name: ")
print("Hello ", person)
is returning an error: NameError: name 'Bob' is not defined.
I have basically just copied and pasted what was from the tutorial at this point, but it still doesn't work unless I put the name in quotation marks. What am I doing wrong?
Your code should work perfectly fine in Python 3. However, in Py2 it will throw a NameError as there are differences between input() and raw_input(). Essentially, input() in Python 2 is the same as eval(raw_input(""Enter name: ")), meaning that it will attempt to run the inputed code as Python.
In Python 3, raw_input() is no more, and input() operates the way you are expecting it to here: Print a line, accept input, and assign it in string format to a variable.
You simply are not using Python 3. In Python 2, input() works differently; see this excerpt from the docs.
Equivalent to eval(raw_input(prompt)).
So, when you type Bob without quotation marks, you are basically saying eval(Bob). With quotes it is eval("Bob"). raw_input() does not exist in Python 3, so it will not be defined if you are running with Python 3.
Make sure that you are running your file with Python 3:
Type which python and which python3 to make sure you have both.
Run python and python3 and see which versions you are running when the python shell opens.
You can check the version of Python which is running your code by doing import sys; print(sys.version).
Run your code via python3 yourfile.py.
As J.F. Sebastian notes, you should add #!/usr/bin/env python3 to the very first line of your file. Chances are, you either wrote #! /usr/bin/env python which on OS X is 2.7.x (I think it's 2.7.2, and I think python is a symlink to the python27 binary) and are running your file just by typing its name, or you are running it through python.
I tried to write my first python program and I already get an error message. In the textbook introduction to computer science using python i found the following code:
name = input('What is your name? ')
print('Hello', name)
print('Welcome to Python!')
I checked multiple times for errors and I'm quite sure i typed it exactly like the textbook states. I saved the program as MyFirstProgram.py and after that i ran the module (by pressing F5). If i understand correctly the program asks you to fill in a name. So i typed 'John'. But when i did, the following error occurs:
Traceback (most recent call last):
File "C:/Users/Wout/.ipython/MyFirstProgram.py", line 3, in <module>
name = input('What is your name? ')
File "<string>", line 1, in <module>
NameError: name 'John' is not defined
Why is 'John' not defined? Isn't it the purpose of the program to enter any name? Why do i have to define it? I followed the instructions to the letter...
Kind regards
input, in Python 2, evaluates the input as if it were a snippet of Python code. This is almost never what you want. Use raw_input instead.
By the way, you're writing your code as if it were Python 3, but you appear to be using a Python 2 interpreter. If you run your code with Python 3, it will work fine (input in Python 3 is the same as raw_input in Python 2).
You should use raw_input() instead of an input(), since you are on python-2.x:
name = raw_input('What is your name? ')
print('Hello', name)
print('Welcome to Python!')
prints:
What is your name? John
('Hello', 'John')
Welcome to Python!
You are following a textbook for Python 3 but using Python 2. In Python 2, must use raw_input and don't need brackets on print statements.
'John' will work with input (John won't work), however you should use raw_input() like the others said