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

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.

Related

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)

running into unidentifiable syntax error [duplicate]

I have a simple Python question that I'm having brain freeze on. This code snippet works. But when I substitue "258 494-3929" with phoneNumber, I get the following error below:
# Compare phone number
phone_pattern = '^\d{3} ?\d{3}-\d{4}$'
# phoneNumber = str(input("Please enter a phone number: "))
if re.search(phone_pattern, "258 494-3929"):
print "Pattern matches"
else:
print "Pattern doesn't match!"
Pattern does not match
Please enter a phone number: 258 494-3929
Traceback (most recent call last):
File "pattern_match.py", line 16, in <module>
phoneNumber = str(input("Please enter a phone number: "))
File "<string>", line 1
258 494-3929
^
SyntaxError: invalid syntax
C:\Users\Developer\Documents\PythonDemo>
By the way, I did import re and tried using rstrip in case of the \n
What else could I be missing?
You should use raw_input instead of input, and you don't have to call str, because this function returns a string itself:
phoneNumber = raw_input("Please enter a phone number: ")
In Python version 2.x, input() does two things:
Reads a string of data. (You want this.)
Then it evaluates the string of data as if it were a Python expression. (This part is causing the error.)
The function raw_input() is better in this situation because it does #1 above but not #2.
If you change:
input("Please enter a phone number: ")
to read:
raw_input("Please enter a phone number: ")
you'll eliminate the error of the phone number not being a valid Python expression.
The input() function has tripped up so many people learning Python that starting with Python versions 3.x, the designers of the language removed the extra evaluation step. This makes input() in versions 3.x behave the same as raw_input() in versions 2.x.
See also a helpful wikibooks article.
The input() function actually evaluates the input that's typed into it:
>>> print str(input("input: "))
input: 258238
258238
>>> print str(input("input: "))
input: 3**3 + 4
31
It's trying to evaluate '258 494-3929' which is invalid Python.
Use sys.stdin.readline().strip() to do your read.
input() calls eval(raw_input(prompt)), so you want phoneNumber = raw_input("Please enter a phone number: ").strip()
See also http://docs.python.org/library/functions.html#input and http://docs.python.org/library/functions.html#raw_input

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 NameError: name 'hello' is not defined [duplicate]

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.

How to define a variable inside the print function?

I am a newbie in this field, and I am trying to solve a problem (not really sure if it is possible actually) where I want to print on the display some information plus some input from the user.
The following works fine:
>>> print (" Hello " + input("tellmeyourname: "))
tellmeyourname: dfsdf
Hello dfsdf
However if I want to assign user's input to a variable, I can't:
>>> print (" Hello ", name = input("tellmeyourname: "))
tellmeyourname: mike
Traceback (most recent call last):
File "<pyshell#47>", line 1, in <module>
print (" Hello ", name = input("tellmeyourname: "))
TypeError: 'name' is an invalid keyword argument for this function
I have researched inside here and other python documentation, tried with %s etc. to solve, without result. I don't want to use it in two lines (first assigning the variable name= input("tellmeyourname:") and then printing).
Is this possible?
Starting from Python 3.8, this will become possible using an assignment expression:
print("Your name is: " + (name := input("Tell me your name: ")))
print("Your name is still: " + name)
Though 'possible' is not the same as 'advisable'...
But in Python <3.8: you can't. Instead, separate your code into two statements:
name = input("Tell me your name: ")
print("Your name is: " + name)
If you often find yourself wanting to use two lines like this, you could make it into a function:
def input_and_print(question):
s = input("{} ".format(question))
print("You entered: {}".format(s))
input_and_print("What is your name?")
Additionally you could have the function return the input s.
no this is not possible. well except something like
x=input("tell me:");print("blah %s"%(x,));
but thats not really one line ... it just looks like it

Categories