Search code examples
python-3.xreturn

Return statement is not returning


Function PoNanswer should return True or False, it is a loop which can be stoped only when you say Yes,... or No,... and when you say it, it should return. But very strangely it is not returning... How to fix it? thank you in advance)

def PoNanswer(voice_data):
  for i in pANSWER_LIST:
    if i in voice_data.split():
        print('true')
        return True
  for i in nANSWER_LIST:
    if i in voice_data.split():
        print('false')
        return False
  voice_data = record_audio('I did not get it. Please repeat')
  PoNanswer(voice_data)




class Command:
  def __init__(self, raw_command):
    self.raw_command = raw_command
    self.key_word = False
    self.action_word = False
    self.processing()
  def processing(self):
    print(self.raw_command)
    for i in self.raw_command.split():

        if i in COMMANDS_LIST:
            self.key_word = i
        elif i in ACTION_WORD_LIST:
            self.action_word = i


    if self.key_word == COMMANDS_LIST[0]:
        if self.action_word:
            speak('ordering...')
            main('-//-')
        else:
            if PoNanswer(record_audio(ADDITIONAL_QUESTIONS[0])):
                self.raw_command = self.raw_command + "order"
                print("mod")
                self.processing()
            else:
                speak('Okay')
                main('-//-')

            self.processing()

Solution

  • In this case, I should have used a loop ( while 1: ) instead of calling the same function inside the function.

    def PoNanswer(voice_data):
    while 1:
        for i in pANSWER_LIST:
            if i in voice_data.split():
    
                return True
        for i in nANSWER_LIST:
            if i in voice_data.split():
    
                return False
        voice_data = record_audio('I did not get it. Please repeat')
    

    This ^ is fortunately working.

    def func(var):
       ...
       func(var)
    

    This ^ wasn't, I have no clue why, but I am happy it is working now))