I'm starting at Python and I was going pretty well until I get to classes part. Well, I'm trying to create a ChatBot, and in the main class the function Python pensar()
returns iniciar
so the function resp()
can append it to self.recente list. It occurs as it has to be, but when the loop gets to pensar()
again, it doesn't get the self.recente[-1]. Hope someone can help me.
Here's the class code:
class IA():
def __init__(self, nome):
self.nome = nome
self.recente = []
def ouvir(self):
iniciar = input('»')
iniciar = iniciar.upper()
iniciar = iniciar.replace('O ', '')
return iniciar
def pensar(self, iniciar):
if iniciar == 'OI':
return 'Ola, qual seu nome?'
if self.recente[-1] == 'Olá, qual seu nome?':
a = self.pegar_nome()
b = self.resp_nome(b)
def pegar_nome(self):
pass
def resp_nome(self, iniciar):
pass
def resp(self, iniciar):
self.recente.append(iniciar)
print(iniciar)
And here's the main.py one:
from Ia import IA
tchau = ['TCHAU', 'XAU', 'ATE LOGO', 'ATÉ LOGO', 'ATE MAIS', 'ATÉ MAIS']
while True:
a = IA('Joao')
b = a.ouvir()
if b in tchau:
print('Até mais')
break
c = a.pensar(b)
a.resp(c)