1

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
Ayush
  • 49
  • 7

1 Answers1

1

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.

U13-Forward
  • 69,221
  • 14
  • 89
  • 114