-1

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)   
Youness Saadna
  • 792
  • 2
  • 8
  • 25
  • 1
    Why are you creating a new instance of `IA` every iteration of the loop? That doesn't look right – UnholySheep Aug 10 '20 at 21:52
  • 1
    Are you getting an error? If so, post the traceback message so we see the details. – tdelaney Aug 10 '20 at 21:54
  • You need to call your function pensar inside the inisiar function. – zenalc Aug 10 '20 at 21:54
  • 1
    `self.recente` starts off as an empty list. If `pensar` is called before `resp`, nothing has been added to the list. `self.recente[-1]` asks for the last value in the list but since its empty you get an index error. `pensar` must account for the case where there isn't anyting recent. So, what would like to happen in that case? – tdelaney Aug 10 '20 at 21:58

2 Answers2

0

The root cause of the problem is in these 2 statements:

  1. self.recente[-1] ---> trying to get an array element that does not exist.

  2. b = self.resp_nome(b) ---> referring to b before it is initialized.

The problem can be fixed in these steps:

  1. Converting the statement into a condition that verifies the presence of specific value in the array.

    I.E., if 'Olá, qual seu nome?' not in self.recente:

  2. Replace self.resp_nome(b) with self.resp_nome(a)

Here is the working example with the 2 changes implemented:

# File name: python-class-demo.py

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 'Olá, qual seu nome?' not in self.recente:
            a = self.pegar_nome()
            b = self.resp_nome(a)
    
        
    def pegar_nome(self):
        pass
    
    
    def resp_nome(self, iniciar):
        pass
    

    def resp(self, iniciar):
        self.recente.append(iniciar)
        print(iniciar)

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)

Output:

>python python-class-demo.py
»as
None
»OU
None
»TCHAU
Até mais
Gopinath
  • 4,066
  • 1
  • 14
  • 16
-1

You may write self variables in OOP:

>> (self.iniciar)

Without self, iniciar is a global variable

sidq
  • 1
  • Polite correction: The variable is limited in scope to the parent class or function; certainly **not** global. – S3DEV Aug 10 '20 at 22:05
  • See [LEGB rule](https://stackoverflow.com/a/292502/2745495). Without `self`, `iniciar` will be scoped to the enclosing function or class. – Gino Mempin Aug 11 '20 at 02:46