0

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()
  • 1
    I don't know if this is the only problem, but you need to return the result of the recursive call: `return PoNanswer(voice_data)`. It would be better just to wrap that in a loop though instead of using recursion. – Carcigenicate Oct 23 '20 at 17:02
  • @EkureEdem `return` in this context is correct, `break` is not. – DYZ Oct 23 '20 at 17:17
  • @Carcigenicate I am not sure what you meant(my knowledge is only practical, not theoretical), but it somehow worked!) Thanks – Nikita Aleksandrov Oct 23 '20 at 17:35
  • @NikitaAleksandrov All paths in a function must result in a value being returned if you are returning values. When `PoNanswer(voice_data)` returns, you don't do anything with the return value, so the function ends, and `None` is returned implicitly. – Carcigenicate Oct 23 '20 at 17:39
  • Does this answer your question? [Why does my recursive function return None?](https://stackoverflow.com/questions/17778372/why-does-my-recursive-function-return-none) – Carcigenicate Oct 23 '20 at 17:40
  • @Carcigenicate absolutely! – Nikita Aleksandrov Oct 23 '20 at 18:49

1 Answers1

0

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))