On my learning phase of python (2.7). I am following a "how to" book, and this is the first thing that has not worked. The topic is interactive inputs.
They give the code, which I think I typed in correctly:
name=input('Enter your name ')
I then execute the line and it gives me the prompt I would expect:
Enter your name
Next to the prompt, I type in my name and press enter. But instead of it creating an object I get an error.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
NameError: name 'Myles' is not defined
I have done this both in IDLE and my IDE (Spyder) and get the same error. So what dumb thing am I doing?
Thanks
Don't use:
name = input(...)
Use:
name = raw_input(...)
Related
I'm new to Python, and I'm trying to compile a VERY simple code in Python (using Atom editor), and after I run this code:
name = input("Name:")
print(f"hello, {name}")
And after typing in any name (in this case I simply typed in n, I get this error message:
Name:n
Traceback (most recent call last):
File "hello_name.py", line 1, in <module>
name = input("Name:")
File "<string>", line 1, in <module>
NameError: name 'n' is not defined
I hope you can help! Thanks!
You need to use raw_input instead of input, because input runs eval on the input. (Only in python 2, in Python 3 input has the same behavior of raw_input and raw_input is removed)
What that means is that after getting your input, python would evaluate your input as if it was code.
Try entering "3+2*5-2" in the input and see the output.
(Note that writing something like:"x = 5" won't work, because eval just evaluates an expression, like y+3 (assuming y is defined) and it does not actually run the code)
(But really, you should use python 3 if you are just learning Python and you are not already used to python2)
yes its right i also phase a problem like that and my code is
name = input("enter you name: ")
print(name)
so i enter my name Anshu
then it show this error
Traceback (most recent call last):
File "/home/anshu/Projects/coding/python/dictnory_problem.py", line 6, in
search = input("enter your keyword: ")
File "", line 1, in
NameError: name 'anshu' is not defined
then i change my code i use
name = raw-input("enter your name")
print(name)
Code:-
input_var=input("please enter the value")
print(input_var)
Error:-
Enter a value
Runtime Exception
Traceback (most recent call last):
File "file.py", line 3, in
n=input("Enter a value")
EOFError: EOF when reading a line
I have started learning Python and tried to run this simple input and print statement. But its giving me the above error. I have tried running it on a online python compiler and it runs fine but when running on a compiler provided in a learning portal I am getting the above error.
I have tried running it on a online python compiler and it runs fine but when running on a compiler provided in a learning portal I am getting the above error.
input simply reads one line from the "standard input" stream. If the learning portal removes access to it (either closes it or sets it as a non-readable stream) then input is going to immediately get an error when it tries to read from the stream.
It simply means you can't use stdin for anything on that platform, so no input(), no sys.stdin.read(), … (so the resolution is "don't do that", it's pretty specifically forbidden)
In this specific case, the learning platform provides a non-readable stream as stdin e.g. /dev/null:
# test.py
input("test")
> python3 test.py </dev/null
Traceback (most recent call last):
File "test.py", line 4, in <module>
input("test")
EOFError: EOF when reading a line
if stdin were closed, you'd get a slightly different error:
> python3 test.py <&-
Traceback (most recent call last):
File "test.py", line 4, in <module>
input("test")
RuntimeError: input(): lost sys.stdin
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
I'm using Python 3.3 and Emacs 23.4 on Windows 7. I'm getting some odd behaviour when using python-shell. If I type in a command that produces some sort of output I get the result immediately on the next line. If the Python statement I've entered causes an error however, no output is shown. When I type in the next Python statement and hit enter, the error message for the previous line will be displayed.
For example when I'm processing some command line arguments:
>>> args
Namespace(templatedir=None, xmldir=None)
>>> args.bobbins
>>> args.templatedir
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'Namespace' object has no attribute 'bobbins'
>>>
The first statement prints out the value of the args variable.
The second statement should print out an error message but nothing is printed.
The third statement is correct but actually prints out the error from the second statement.
Does anybody have any idea what's wrong with my Python / Emacs setup?
This was a bug in Python, and was fixed.
I'm using Python 3.3 and Emacs 23.4 on Windows 7. I'm getting some odd behaviour when using python-shell. If I type in a command that produces some sort of output I get the result immediately on the next line. If the Python statement I've entered causes an error however, no output is shown. When I type in the next Python statement and hit enter, the error message for the previous line will be displayed.
For example when I'm processing some command line arguments:
>>> args
Namespace(templatedir=None, xmldir=None)
>>> args.bobbins
>>> args.templatedir
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'Namespace' object has no attribute 'bobbins'
>>>
The first statement prints out the value of the args variable.
The second statement should print out an error message but nothing is printed.
The third statement is correct but actually prints out the error from the second statement.
Does anybody have any idea what's wrong with my Python / Emacs setup?
This was a bug in Python, and was fixed.