我在第 44 行的 "If Statement" 和我在第 37 行添加到变量语句有什么问题?

What is wrong with my "If Statement" in line 44 and my addition to variable statement in line 37?

我目前正在制作一个语音控制器的粗略轮廓,所以我可以停止设计零件,但即使在制作粗略轮廓时,我 运行 也遇到了一些问题,我在 if 语句和语法方面的技能水平很生疏变量加法。具体的错误是程序在我第一次询问脚本如何执行而不是继续 运行 之后退出,如果我删除带有变量添加的行以及关于不同语句的块被问过一次后替换旧的“你好吗”。

from time import ctime
import time
import os
import pyttsx3
import random


repetitionsocial1=0

numberList = ["Thanks for asking. But I am a computer","222","I eat poop","444","555"]

def recordAudio():
    # Record Audio
    r = sr.Recognizer()
    with sr.Microphone() as source:
     print("Say something!")
     audio = r.listen(source)

    # Speech recognition using Google Speech Recognition
    data = ""
    try:
        # Uses the default API key
        # To use another API key: `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
        data = r.recognize_google(audio)
        print("You said: " + data)
    except sr.UnknownValueError:
        print("Offline Recognition could not understand audio")
    except sr.RequestError as e:
        print("Could not request results from Offline Recognition service; {0}".format(e))

    return data

def jarvis(data):
    global repetitionsocial1
    if "how are you" in data and repetitionsocial1==0 :
        repetitionsocial1=repetitionsocial1+1
        engine = pyttsx3.init()
        engine.say("What answer do you expect. I am a Computer?")
        engine.runAndWait()
        
        

    if "how are you" in data and repetitionsocial1>=0:
        engine = pyttsx3.init()
        engine.say("I am still fine, but again, I am a computer. You have asked me this"+str(repetitionsocial1)+"times")
        engine.runAndWait()

    if "what time is it" in data:
        engine = pyttsx3.init()
        engine.say(ctime())
        engine.runAndWait()

   # if "where is" in data:
    #    data = data.split(" ")
     #   location = data[2]
      #  speak("Hold on Frank, I will show you where " + location + " is.")
       # os.system("chromium-browser https://www.google.nl/maps/place/" + location + "/&")

# initialization
time.sleep(2)
engine = pyttsx3.init()
engine.say("Hi Frank, what can I do for you?")
engine.runAndWait()

while 1:
    data = recordAudio()
    jarvis(data)

看看这两个片段。

    if "how are you" in data and repetitionsocial1==0 :
        repetitionsocial1=repetitionsocial1+1
        ...
    if "how are you" in data and repetitionsocial1>=0:
        ...

当第一个条件为真时,它将 repetitionsocial1 加 1,因此现在该变量包含 1。因此,第二个条件也将成立,因为 1>=0.

当您有互斥条件并且不想考虑第一个条件导致的变量变化时,您应该使用 elif