NameError: name 'ana' is not defined

4

I'm a beginner in python and needed to do a program that checks if a given word is palindromous. The program would then 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 through 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 type variables in python, does anyone know why this happens?

    
asked by anonymous 13.07.2016 / 04:15

3 answers

4

It turns out that you are trying to run a code made for Python 3. * using Python 2. *. Change input to raw_input that will work.

You'll probably need to change the comments to be able to use the accents.

#!/usr/bin/env python
# -*- coding: utf-8 -*-

palavra = raw_input ('Palavra: ')
if palavra == palavra[::-1]:
   print ("%s é palindrome" %palavra)
else:
   print ("%s não é palindrome" %palavra)

input() in Python 2. * takes the value entered by the user and tries to "evaluate" it (it's like trying to interpret the string as code itself). It's the same thing as eval(raw_input()) .

In Python 3. *, input() does the same as the old raw_input .

    
13.07.2016 / 04:37
2

I believe you want to use the raw_input function, in fact the input function in version 2.7 of python works differently, see an example:

>>> d="eder"
>>> palavra = input("teste:")
teste:d
>>> palavra
'eder'

It expects something that python can interpret in code, in this case I had declared the variable d getting my name.

let's test the function raw_input now:

>>> palavra = raw_input('Palavra: ')
Palavra: eder
>>> palavra
'eder'

So raw_input seems to be what you need ...

    
13.07.2016 / 04:41
0

I'm new to Python. I do not want to give up, so I need to help me fix the error:

Here is the code:

quiz_eval = tf_quiz ("The octupos have green blood", "f")

print ("The answer is", quiz_eval)

When I run the program I get the error: the name quiz_eval 'is not defined'

    
29.10.2017 / 14:13