7
I am a beginner in python and I needed to make a program that would verify if a given word is palindromo. The program then would be:
#encoding: utf-8
palavra = input ('Palavra: ')
if palavra == palavra[::-1]:
print ("%s é palindrome" %palavra)
else:
print ("%s não é palindrome" %palavra)
The problem is that when I try to open the program from the terminal (use Ubuntu 16.04), the following error appears:
Traceback (most recent call last):
File "palindrome.py", line 3, in <module>
palavra = input ('Palavra: ')
File "<string>", line 1, in <module>
NameError: name 'ana' is not defined
And I have no idea what that means. This has happened whenever I need to work as string variables in python, someone knows why it happens?
the way he declared the text encoding of the Python file (
#encoding: utf-8
) works too. No need to put the-*-
- they are only present in many examples and documents because in this way the coding was also recognized by older versions of EMACS.– jsbueno
Thanks for the @jsbueno add-on. I didn’t know it worked the other way because I always see it the way I did...
– Jéf Bueno
It uses a regular expression, and looks for
coding: codifição
on the first or second line, with or without spaces after two dots. In Python3 it is assumed that the encoding is utf-8, making the line unnecessary.– jsbueno