Python NameError: name 'hello' is not defined [duplicate] - python

This question already has answers here:
input() error - NameError: name '...' is not defined
(15 answers)
Closed 7 years ago.
I get a NameError when I attempt to execute the following code with any possible input string in Python 3.0:
def onePerLine(str):
for i in str:
print(i)
word=input("Enter a phrase or word: ")
onePerLine(word)
The error is as follows:
Enter a phrase or word: hello
Traceback (most recent call last):File"C:\Users\R\Documents\Python30\func2.py",line 5, in <module> word=input("Enter a phrase or word: ")
File "<string>", line 1, in <module>
NameError: name 'hello' is not defined
How do i fix this and get my code to run?
PS: I'm a newbie to python and to programming in general. Any assistance would be appreciated.

You are using Python 2, so you need to use raw_input
>>> x = input('')
hello
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
x = input('')
File "<string>", line 1, in <module>
NameError: name 'hello' is not defined
Using raw_input
>>> x = raw_input('')
hello
>>> x
'hello'

You are using python 2 and you need to use raw_input instead of input which evaluate the string and assumes it as a variable name.
input([prompt])
Equivalent to eval(raw_input(prompt)).
...
Consider using the raw_input() function for general input from users.

Related

How to fix "String reversing" code in python? [duplicate]

This question already has answers here:
input() error - NameError: name '...' is not defined
(15 answers)
Closed 3 years ago.
My code for reversing a string works on other websites but is not working on my ubuntu machine on vim.
wrd=input("Please enter a word ")
wrd=str(wrd)
rvs=wrd[::-1]
print(rvs)
if wrd == rvs:
print("This word is a palindrome")
else:
print("This word is not a palindrome")
It gives the error:
python hannah1.py
Please enter a word hannah
Traceback (most recent call last):
File "hannah1.py", line 1, in <module>
wrd=input("Please enter a word")
File "<string>", line 1, in <module>
NameError: name 'hannah' is not defined
You have to use raw_input:
wrd=raw_input("Please enter a word")
rvs=wrd[::-1]
print(rvs)
if wrd == rvs:
print("This word is a palindrome")
else:
print("This word is not a palindrome")
Now it will work, input in Python 2 is the same as eval(input(...)) in Python 3, however it will try to look for a variable called hannah, while there isn't any variable called hannah.

NameError when assigning input() to variable (Python2.7.11) [duplicate]

This question already has answers here:
Differences between `input` and `raw_input` [duplicate]
(3 answers)
Closed 3 years ago.
I get a name error even though I'm just trying to put a string into a variable.
I am trying to do this on Python 2.7.11. Does anyone have anything that helps? Upgrading Python is not an option for me.
def translate(phrase):
translation = ""
for letter in phrase:
if letter in " ":
translation = translation + "#"
result = (input("enter a phrase you want encrypted: "))
result = translate(result)
This is the error that's shown:
Traceback (most recent call last):
File "D:\hello\encrydecry\encryption1.py", line 158, in <module
>
result = (input("enter a phrase you want encrypted: "))
File "<string>", line 1, in <module>
NameError: name 'hello' is not defined
In Python 2, when you use input(), Python interprets the input. So when you type hello, hello is interpreted as a variable, and you're essentially doing result = hello. Hence the error NameError: name 'hello' is not defined.
One option is to simply type the input between quotes, so it will be interpreted as a string: 'hello'.
To avoid the input being interpreted altogether, you have to use raw_input() instead of input(), which doesn't interpret the user input and always returns a string:
result = raw_input("enter a phrase you want encrypted: ")
def translate(phrase):
translation = ""
for letter in phrase:
if letter in " ":
translation = translation + "#"
result =raw_input("enter a phrase you want encrypted: ")
result = translate(result)

Simple python code is not working [duplicate]

This question already has answers here:
input() error - NameError: name '...' is not defined
(15 answers)
Closed 5 years ago.
The following code is not working:
person = input('Enter your name: ')
print('Hello', person)
Instead of printing Hello <name> it is giving me the following traceback:
Traceback (most recent call last):
File "C:/Users/123/Desktop/123.py", line 1, in <module>
person = input('Enter your name: ')
File "<string>", line 1, in <module>
NameError: name 'd' is not defined
To read strings you should use:
person = raw_input("Enter your name: ")
print('Hello', person)
When you use input it reads numbers or refers to variables instead. This happens when you are using Python 2.7 or below. With Python 3 and above you have only input function.
Your error states that you entered "d" which is a variable not declared in your code.
So if you had this code instead:
d = "Test"
person = input("Enter your name: ")
print('Hello', person)
And you type now "d" as name, you would get as output:
>>>
('Hello', 'Test')
What is the error?
You used this:
person = input('Enter your name: ')
You should have used this:
person = raw_input('Enter your name: ')
Why these are different
input tries to evaluate what is passed to it and returns the value, whereas raw_input just reads a string, meaning if you want to just read a string you need to use raw_input
In Python 3 input is gone, and raw_input is now called input, although if you really want the old behaviour exec(input()) has the old behaviour.

python convert string "0" to float gives error

i have the following code in my python script, to launch an application and grab the output of it.
An example of this output would be 'confirmed : 0'
Now i only want to know the number, in this case zero, but normally this number is float, like 0.005464
When i run this code it tells me it cannot convert "0" to float. What am i doing wrong?
This is the error i get now:
ValueError: could not convert string to float: "0"
cmd = subprocess.Popen('/Applications/Electrum.app/Contents/MacOS/Electrum getbalance', shell=True, stdout=subprocess.PIPE)
for line in cmd.stdout:
if "confirmed" in line:
a,b=line.split(': ',1)
if float(b)>0:
print "Positive amount"
else:
print "Empty"
According to the exception you got, the value contained in b is not 0, but "0" (including the quotes), and therefore cannot be converted to a float directly. You'll need to remove the quotes first, e.g. with float(b.strip('"')).
As can be seen in the following examples, the exception description does not add the quotes, so they must have been part of the original string:
>>> float('"0"')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: could not convert string to float: "0"
>>> float('a')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: could not convert string to float: a
I have tested the code and found that split(': ', 1) result contains string
>>> line = "1: 456: confirmed"
>>> "confirmed" in line
True
>>> a,b=line.split(': ', 1)
>>> a
'1'
>>> b
'456: confirmed'

Python parse comma-separated number into int [duplicate]

This question already has answers here:
How to convert a string to a number if it has commas in it as thousands separators?
(10 answers)
Closed 11 months ago.
How would I parse the string 1,000,000 (one million) into it's integer value in Python?
>>> a = '1,000,000'
>>> int(a.replace(',', ''))
1000000
>>>
There's also a simple way to do this that should handle internationalization issues as well:
>>> import locale
>>> locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
'en_US.UTF-8'
>>> locale.atoi("1,000,000")
1000000
>>>
I found that I have to explicitly set the locale first as above, otherwise it doesn't work for me and I end up with an ugly traceback instead:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.6/locale.py", line 296, in atoi
return atof(str, int)
File "/usr/lib/python2.6/locale.py", line 292, in atof
return func(string)
ValueError: invalid literal for int() with base 10: '1,000,000'
Replace the ',' with '' and then cast the whole thing to an integer.
>>> int('1,000,000'.replace(',',''))
1000000

Categories