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)
grizzthedj
  • 7,131
  • 16
  • 42
  • 62
Guilherme
  • 3
  • 7
  • Possible duplicate of [How to read Unicode input and compare Unicode strings in Python?](https://stackoverflow.com/questions/477061/how-to-read-unicode-input-and-compare-unicode-strings-in-python) – hostingutilities.com Jan 31 '18 at 18:34
  • 1
    Possible duplicate of [NameError from Python input() function](https://stackoverflow.com/questions/16457441/nameerror-from-python-input-function) – tripleee Feb 02 '18 at 17:24

1 Answers1

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.

Adam Barnes
  • 2,922
  • 21
  • 27
  • Good advice, but doesn't answer the question. – Mark Ransom Feb 02 '18 at 17:16
  • 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. – Adam Barnes Feb 02 '18 at 17:19
  • Well it is a little difficult to tell because the line generating the UnicodeEncodeError is never identified. Maybe you're right. – Mark Ransom Feb 02 '18 at 17:24
  • `'\xe1' == 'á'`, so it's the line I identified. – Adam Barnes Feb 02 '18 at 17:26
  • Thanks, I'm using python 2.7. This is a bot, or should be, a bot learning the conversation accordingly with the given training. In this case, "talk" was the data entry that he should recognize, "hi", and answer with the other options, but I do not know why it's giving error, I've already researched these problems here, until I try some solutions but not It worked. I wanted to make the same video on youtube: https://www.youtube.com/watch?v=VtF-Ucj4aAM&t=278s – Guilherme Feb 02 '18 at 20:39
  • _Why_ are you using Python 2.7 though? This is a new project. Do not use Python 2. Even in the video, at the exact time you linked, he is running the program with Python 3. – Adam Barnes Feb 02 '18 at 20:42
  • I'm not using python 3 because it did not come with the pip installed. – Guilherme Feb 02 '18 at 20:47
  • I'm going to try reinstalling python 3, – Guilherme Feb 02 '18 at 20:49
  • Python 3 comes with pip as the command `pip3`, but in your travels, look into `virtualenv`. Good luck. – Adam Barnes Feb 02 '18 at 20:51
  • 1
    it worked, I installed python 3.4 because it already comes with the pip, and it worked. Thank you. – Guilherme Feb 02 '18 at 22:53