jeudi 14 mars 2019

python-chatbot does not execute if-statements properly

I'm trying to code a voice-enabled, list-based chatbot in python. Unfortunately, python runs the code until the first if-statement. The error: "if data in chips_small: TypeError: 'in ' requires string as left operand, not NoneType" occurs. Do you have any suggestions for me concerning the correct execution of if-statements? It automatically executes the first function, until the error occurs. Thank you very much.

Here's my code:

 chips =  ("chips")
 chips_re =  ("Great choice! Our chips are delicious! Do you want a small or a large box?")    

   def chips(data):
        with sr.Microphone() as source:
            r = sr.Recognizer()
            r.pause_threshold = 1
            r.adjust_for_ambient_noise(source)
            audio = r.listen(source)

            print(chips_re)
            engine.say(chips_re)
            engine.runAndWait()

        if data in chips_small:
            print('You said: ' + data)
            print(chips_small_re)
            engine.say(chips_small_re)
            engine.runAndWait()

        elif data in chips_large:
            print('You said: ' + data)
            print(chips_large_re)
            engine.say(chips_large_re)
            engine.runAndWait()

        else:
            print(error)
            engine.say(error)
            engine.runAndWait

        return data


"""the same for salad and burger"""

    while True:
        data = recordAudio()
        chat(data)
        if data in chips(data):
            print('You said: ' + data)
            chips(data)

        elif data in salad(data):
            print('You said: ' + data)
            salad(data)

        elif data in burger(data):
            print('You said: ' + data)
            burger(data)       

Aucun commentaire:

Enregistrer un commentaire