This question already has answers here:
input() error - NameError: name '...' is not defined
(15 answers)
Closed 7 years ago.
I'm a Python newbie and I get the following error for the code...
name = input("What's your name? ")
print(name)
Output:
G:\Code\Python>UserInput.py
What's your name? Jeremy
Traceback (most recent call last):
File "G:\Code\Python\UserInput.py", line 3, in <module>
name = input("What's your name? ")
File "<string>", line 1, in <module>
NameError: name 'Jeremy' is not defined
The code gets executed only if I replace the input() as raw_input... How do I get it to display the message just by including the input()? I get that it has got something to do with the Python Interpreter versions (I've got both python27 and python34 installed). How do I go on about that??
You shouldn't be using input, it is equivalent to eval(raw_input(prompt)) which will try to execute your input.
https://docs.python.org/2/library/functions.html#input
Use raw_input instead.
Related
This question already has answers here:
Error saying that name 'math' is not defined when trying to use asin()
(3 answers)
Python : name 'math' is not defined Error?
(4 answers)
Closed 7 months ago.
I have written this code in sublime text
if intentos<3:
solution= math.sqrt(number)
print("La raiz cuadrada de " + str(number) + "es" + str(solution))
Then I have used sublimeREPL and I don't know why but It has appear this:
Traceback (most recent call last):
File "prueba.py", line 23, in <module>
solution= math.sqrt(number)
NameError: name 'math' is not defined
This error only appears when I use math.sqrt and I don not understand why. Moreover, I have copied this exact code from a video tutorial and he does the same as me but for some reason his sublimeREPL works correctly.
It gives the error name 'math' is not defined because you haven't defined math.
You need to import it using:
import math
at the start of the program.
Read more in the documentation
You miss an import of the math module, please try to add the line at the beginning of your script
import math
This question already has an answer here:
How to get bash arguments with leading pound-sign (octothorpe)
(1 answer)
Closed 1 year ago.
I am trying to pass a command line argument like this because the actual string contains a # symbol before and after it
python3 test.py #fdfdf#
This is the error message I am getting
Traceback (most recent call last):
File "test.py", line 3, in <module>
print(sys.argv[1])
IndexError: list index out of range
How can I pass a string which contains a # symbol in the beginning and the end as a command line argument?
Update:
OS: Ubuntu 20.04
You can try one of these and see which one works:
python3 test.py "#fdfdf#" or python3 test.py \#fdfdf\#
This question already has answers here:
input() error - NameError: name '...' is not defined
(15 answers)
Closed 5 years ago.
I am fairly new in python and I am writing a python script that can read json files from a directory and work on it. For that purpose, I am asking the user about the file name.
file_dir = input("Please enter the directory COMPLETE path where all dialog json files are placed: ")
files = listdir(str(file_dir))
However, I get the error when I enter the path.
Please enter the directory COMPLETE path where all dialog json files are placed: /Users/monideepde/Documents/Sanofi/EnglishDialogs
Traceback (most recent call last):
File "/Users/monideepde/PycharmProjects/KoreDialogLanguageConverter/Converter.py", line 4, in <module>
file_dir = input("Please enter the directory COMPLETE path where all dialog json files are placed: ")
File "<string>", line 1
/Users/monideepde/Documents/Sanofi/EnglishDialogs
^
SyntaxError: invalid syntax
Process finished with exit code 1
Strangely, this error is not seen when I wrap my path in double-quotes. If anyone understands why I am getting this error can you please share it with me?
P.S: I am using python 2.7
Thanks
In Python 2.x, input() tries to run the input as a Python expression. Use raw_input() instead - it returns a string without evaluation
file_dir = raw_input("Please enter the directory COMPLETE path where all dialog json files are placed: ")
Try raw_input instead of input since you are using python2.7. raw_input("enter the directory").
And for working with directories check out the os module.
This question already has answers here:
How do I use raw_input in Python 3?
(9 answers)
Closed 7 years ago.
I see that error when i want to debug my python file:
C:\py>python optr_assigment.py<br>
**Traceback (most recent call last):<br>
File "optr_assigment.py", line 43, in <module><br>
aplikasi = DemoOperator()<br>
File "optr_assigment.py", line 8, in __in<br>
x = raw_input("Masukkan nilai x: ")<br>
NameError: name 'raw_input' is not defined**
Python 3 changed raw_input to input.
How do I use raw_input in Python 3
This question already has answers here:
error in python d not defined. [duplicate]
(3 answers)
Closed 8 years ago.
I have been learning how to program in Python using the book "Python the Absolute Beginners Guide." The problem I am having is that when using eclipse-pydev it won't allow me to use the if statement. Here is the code I have written...
name = input("What is your name? ")
print(name)
print("Hello" name )
The result was
What is your name? caleb
Traceback (most recent call last):
File "/Users/calebmatthias/Document/workspace/de.vogella.python.first/simpprogram.py", line 6, in <module>
name = input("What is your name? ")
File "/Users/calebmatthias/Desktop/eclipse 2/plugins/org.python.pydev_2.2.3.2011100616/PySrc/pydev_sitecustomize/sitecustomize.py", line 210, in input
return eval(raw_input(prompt))
File "<string>", line 1, in <module>
NameError: name 'caleb' is not defined
When I do my if statement I put
name = input("What is your name? ")
if name == ("Caleb"):
print(" Hello Bud!")
The result was
What is your name? Caleb
Traceback (most recent call last):
File "/Users/calebmatthias/Document/workspace/de.vogella.python.first/simpprogram.py", line 6, in <module>
name = input("What is your name? ")
File "/Users/calebmatthias/Desktop/eclipse 2/plugins/org.python.pydev_2.2.3.2011100616/PySrc/pydev_sitecustomize/sitecustomize.py", line 210, in input
return eval(raw_input(prompt))
File "<string>", line 1, in <module>
NameError: name 'Caleb' is not defined
Use raw_input instead of input.
Python developers probably should have renamed those functions so it's more clear and beginners don't get confused so easily.
When you type caleb into the prompt with input it's trying to evaluate caleb which looks like a variable. The variable caleb hasn't been defined, so it's raising that exception.
The reason is that you are using the input function which expects that the user will input a string that can evaluate to a python expression. Try changing it to raw_input which will not try to eval, but rather give you a raw string.
Also, try just doing your print statement like: print "Hello", name You were missing a comma sep in that first example.
>>> help(input)
input([prompt]) -> value
Equivalent to eval(raw_input(prompt)).
Use raw_input.