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.
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
I've written this program that I've been making for a couple of weeks, but I've ran into a small problem.
basically, when it asks input ("Do you want to like or dislike the video?: ") I do exec dislike but it doesn't let me use exec?
This is the error im getting
Do you want to like or dislike the video?: exec dislike
Traceback (most recent call last):
File "C:\Users\MrCoInSanity\Desktop\python scripts\Youtube Bot\Youtube bot.py", line 40, in <module>
input("Do you want to like or dislike the video?: ")
File "<string>", line 1
exec dislike
^
SyntaxError: invalid syntax
This exception commonly arises from input having \r in it. Given value is the variable holding the user's input, try with this before passing it to exec:
value = value.replace('\r', '')
I have written a python program that uses values from text files. In order to get the text files, one of the requirements is that it must be able to accept the file path as an argument in the terminal. I am trying to use:
# -*- coding: utf-8 -*-
import numpy
x = str(input("Enter directory path: \n"))
data = numpy.loadtxt(open(x), int)
However , when I run the program in the terminal like this:
MBP:Game test$ python GameOfLife.py
Enter directory path:
/Users/test/Google Drive/Game.py
I get the following error afterwards.
Traceback (most recent call last):
File "Game.py", line 5, in <module>
x = str(input("Enter directory path\n"))
File "<string>", line 1
/Users/test/Google Drive/Game.py
^
SyntaxError: invalid syntax
I am new to python, so any help would be really great.
string must be put in quotes;
Try this:
"/Users/test/Google Drive/Game.py"
In addition you can use function raw_input instead of input. input function run your input text as python code but with raw_input you could input anything without any constraints.
so you can use
x = raw_input("Enter directory path: \n")
instead of
x = str(input("Enter directory path: \n"))
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.
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