0

This is my robot code in Python 2.7:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from chatterbot.trainers import ListTrainer
from chatterbot import ChatBot
bot = ChatBot('Test')
Conversa =['Oi','Olá','Tudo bem?','Eu estou bem']
bot.set_trainer(ListTrainer)
bot.train(Conversa)
while True:
      quest = input('Voce:  ')
      resposta = bot.get_response(quest)
      print ('Bot: ', resposta)

When I run it gives the following error:

Traceback (most recent call last):
  File "file/bot.py", line 15, in <module>
    quest = input('Voce:  ')
  File "<string>", line 1, in <module>
NameError: name 'oi' is not defined

If I change the input to raw_input it gives this error too:

UnicodeEncodeError: 'ascii' codec can't encode character u'\xe1' in position 18: ordinal not in range(128)
0

1 Answer 1

1

Is there a reason you're not using Python 3? One of the major reasons for the version was the complete removal of all these weird problems.

If you have any string which contains a character that's not in the ASCII codepage, you'll need to use a unicode string, instead of a bytestring.

In your case, it will look like this:

Conversa =['Oi',u'Olá','Tudo bem?','Eu estou bem']

Though a better solution, if you absolutely positively must start a new project on Python 2, add from __future__ import unicode_literals at the top of your file.

Sign up to request clarification or add additional context in comments.

10 Comments

Good advice, but doesn't answer the question.
You sure about that? He says with raw_input he has a unicode error. This is the fix to that. As for his error saying 'oi' is not defined, I have no idea why that is because it's not mentioned anywhere in his snippet, and he appears to have moved on from it, so I didn't address it.
Well it is a little difficult to tell because the line generating the UnicodeEncodeError is never identified. Maybe you're right.
'\xe1' == 'á', so it's the line I identified.
it worked, I installed python 3.4 because it already comes with the pip, and it worked. Thank you.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.