python程序。苹果询问机

python程序。苹果询问机,python,if-statement,Python,If Statement,我一直在尝试创建一个故障排除系统,我的代码在乞讨中运行良好,但我似乎找不到问题,因为它停止工作 print("welcome to Apple online iPhone help system, I will ask you a series of questions and please answer with a 'yes' or 'no', if by the end our automatic troubleshooting system does not help you,there

我一直在尝试创建一个故障排除系统,我的代码在乞讨中运行良好,但我似乎找不到问题,因为它停止工作

print("welcome to Apple online iPhone help system, I will ask you a series of questions and please answer with a 'yes' or 'no', if by the end our automatic troubleshooting system does not help you,there are more support and help options available on our website https://getsupport.apple.com/ ") 
q1 = input("Is your phone running slow?")
if q1 == "yes" :
        print("ou need to free up space, phones slow down when they are full, try deleting unused apps or unnecessary photos or buy more storage. If that doesn’t fix your problem contact Apple for more help.")

if q1== "no":
                print("okay let's move on to the next question")
                q2 = input ("have your dropped your phone in water?")
                if q2 == "yes":
                            print ("turn off your phone and leave your phone in a bag of rice for 72 hours and after the waiting periodturn it on and it should work, if it doesn't your phone is broken, contact apple to get it fixed")

                            if q2 == "no":# after this code it stops working i do not understand why.
                                    print ("okay let's move on to the next question")
                                    q3 = input ("Does your phone keep crashing?")
                                    if q3 == "yes":
                                                print ("Reset your phone and that should prevent your phone from crashing, If that doesn’t fix your problem contact Apple for more help.")
                                                if q3 == "no":
                                                        print("okay let's move on to the next question")
                                                        q4 = input("is your promblem due to the recent update?")
                                                        if q4 == "yes":
                                                                    print ("there could be multiple soulitions to the this problem, go to 'www.apple.suppot/recentupdate.com and find your problem")
                                                                    if q4 == "no":
                                                                            print ("okay let's move on to the next question")

当我运行程序时,会发生以下情况:

欢迎使用苹果在线iPhone帮助系统,我会问你一系列问题,请回答“是”或“否”,如果到最后我们的自动故障排除系统对你没有帮助,我们的网站上会提供更多支持和帮助选项
您的手机运行缓慢吗?否
好的,让我们继续下一个问题
你的手机掉在水里了吗?没有


它不会问下一个问题为什么?

检查缩进

if q2 == "no":
def main():
    print("welcome to Apple online iPhone help system, I will ask you a series of questions and please answer with a 'yes' or 'no', if by the end our automatic troubleshooting system does not help you,there are more support and help options available on our website https://getsupport.apple.com/ ")

    q1 = input("Is your phone running slow? ")
    if q1 == "yes" :
        print("you need to free up space, phones slow down when they are full, try deleting unused apps or unnecessary photos or buy more storage. If that doesn’t fix your problem contact Apple for more help.")
        return

    print("okay let's move on to the next question")
    q2 = input("have your dropped your phone in water? ")
    if q2 == "yes":
        print("turn off your phone and leave your phone in a bag of rice for 72 hours and after the waiting periodturn it on and it should work, if it doesn't your phone is broken, contact apple to get it fixed")
        return

    print("okay let's move on to the next question")
    q3 = input("Does your phone keep crashing? ")
    if q3 == "yes":
    print("Reset your phone and that should prevent your phone from crashing, If that doesn’t fix your problem contact Apple for more help.")
        return

    print("okay let's move on to the next question")
    q4 = input("is your problem due to the recent update? ")
    if q4 == "yes":
        print("there could be multiple solutions to the this problem, go to 'www.apple.support/recentupdate.com and find your problem")
        return         

    print("okay let's move on to the next question")
    # the rest of your questions/answers


if __name__ == '__main__':
    main()

缩进错误。其余的情况也一样。此外,如果使用elif,最好使用

以下是正确的代码版本:

print("welcome to Apple online iPhone help system, I will ask you a series of questions and please answer with a 'yes' or 'no', if by the end our automatic troubleshooting system does not help you,there are more support and help options available on our website https://getsupport.apple.com/ ") 
q1 = input("Is your phone running slow?")
if q1 == "yes" :
    print("You need to free up space, phones slow down when they are full, try deleting unused apps or unnecessary photos or buy more storage. If that doesn’t fix your problem contact Apple for more help.")
elif q1 == "no":
    print("okay let's move on to the next question")
    q2 = input ("have your dropped your phone in water?")
    if q2 == "yes":
                print ("turn off your phone and leave your phone in a bag of rice for 72 hours and after the waiting periodturn it on and it should work, if it doesn't your phone is broken, contact apple to get it fixed")

    elif q2 == "no":
            print ("okay let's move on to the next question")
            q3 = input ("Does your phone keep crashing?")
            if q3 == "yes":
                        print ("Reset your phone and that should prevent your phone from crashing, If that doesn’t fix your problem contact Apple for more help.")
            elif q3 == "no":
                    print("okay let's move on to the next question")
                    q4 = input("is your promblem due to the recent update?")
                    if q4 == "yes":
                                print ("there could be multiple soulitions to the this problem, go to 'www.apple.suppot/recentupdate.com and find your problem")
                                if q4 == "no":
                                        print ("okay let's move on to the next question")
使用
elif
块管理逻辑流


您的代码在
if
块中具有
else
功能,因此它们将永远不会执行。

我将免除对答案为“否”的测试,除了“是”或“否”之外,您没有任何意外情况。例如:

print("welcome to Apple online iPhone help system, I will ask you a series of questions and please answer with a 'yes' or 'no', if by the end our automatic troubleshooting system does not help you,there are more support and help options available on our website https://getsupport.apple.com/ ")

q1 = input("Is your phone running slow? ")
if q1 == "yes" :
    print("you need to free up space, phones slow down when they are full, try deleting unused apps or unnecessary photos or buy more storage. If that doesn’t fix your problem contact Apple for more help.")

else:
    print("okay let's move on to the next question")
    q2 = input("have your dropped your phone in water? ")
    if q2 == "yes":
        print ("turn off your phone and leave your phone in a bag of rice for 72 hours and after the waiting periodturn it on and it should work, if it doesn't your phone is broken, contact apple to get it fixed")

    else:            
        print("okay let's move on to the next question")
        q3 = input("Does your phone keep crashing? ")
        if q3 == "yes":
            print ("Reset your phone and that should prevent your phone from crashing, If that doesn’t fix your problem contact Apple for more help.")
        else:
            print("okay let's move on to the next question")
            q4 = input("is your problem due to the recent update? ")
            if q4 == "yes":
                print("there could be multiple solutions to the this problem, go to 'www.apple.support/recentupdate.com and find your problem")
            else:
                print("okay let's move on to the next question")
print("Welcome to Apple online iPhone help system, I will ask you a series "
      "of questions and please answer with a 'yes' or 'no', if by the end "
      "our automatic troubleshooting system does not help you,there are more "
      "support and help options available on our website "
      "https://getsupport.apple.com/ \n")

q_and_a = [
         ["Is your phone running slow? ",
          "You need to free up space, phones slow down when they are full,"  
          " try deleting unused apps or unnecessary photos or buy more storage."
          " If that doesn’t fix your problem contact Apple for more help."],

         ["Have your dropped your phone in water? ",
          "turn off your phone and leave it in a bag of rice for 72 hours "
          "and after the waiting periodturn it on and it should work, if it "
          "doesn't your phone is broken, contact Apple to get it fixed"],

         ["Does your phone keep crashing? ",
          "Reset your phone and that should prevent your phone from crashing. "
          "If that doesn’t fix your problem contact Apple for more help."],

         ["Is your problem due to the recent update? ",
          "There could be multiple solutions to the this problem, "
          "go to 'www.apple.support/recentupdate.com and find your problem"],

          ]

def askit(question):
    retn = False

    ans = input(question[0])
    if ans == "yes":
        print(question[1])
        retn = True
    else:
        print("\nOkay let's move on to the next question")

    return retn

for q in q_and_a:
    if askit(q):
        break
但是,我们可以通过使用列表使代码更易于维护。还要注意,相邻的字符串是连接在一起的——您不需要那些难以编辑的很长的行

例如:

print("welcome to Apple online iPhone help system, I will ask you a series of questions and please answer with a 'yes' or 'no', if by the end our automatic troubleshooting system does not help you,there are more support and help options available on our website https://getsupport.apple.com/ ")

q1 = input("Is your phone running slow? ")
if q1 == "yes" :
    print("you need to free up space, phones slow down when they are full, try deleting unused apps or unnecessary photos or buy more storage. If that doesn’t fix your problem contact Apple for more help.")

else:
    print("okay let's move on to the next question")
    q2 = input("have your dropped your phone in water? ")
    if q2 == "yes":
        print ("turn off your phone and leave your phone in a bag of rice for 72 hours and after the waiting periodturn it on and it should work, if it doesn't your phone is broken, contact apple to get it fixed")

    else:            
        print("okay let's move on to the next question")
        q3 = input("Does your phone keep crashing? ")
        if q3 == "yes":
            print ("Reset your phone and that should prevent your phone from crashing, If that doesn’t fix your problem contact Apple for more help.")
        else:
            print("okay let's move on to the next question")
            q4 = input("is your problem due to the recent update? ")
            if q4 == "yes":
                print("there could be multiple solutions to the this problem, go to 'www.apple.support/recentupdate.com and find your problem")
            else:
                print("okay let's move on to the next question")
print("Welcome to Apple online iPhone help system, I will ask you a series "
      "of questions and please answer with a 'yes' or 'no', if by the end "
      "our automatic troubleshooting system does not help you,there are more "
      "support and help options available on our website "
      "https://getsupport.apple.com/ \n")

q_and_a = [
         ["Is your phone running slow? ",
          "You need to free up space, phones slow down when they are full,"  
          " try deleting unused apps or unnecessary photos or buy more storage."
          " If that doesn’t fix your problem contact Apple for more help."],

         ["Have your dropped your phone in water? ",
          "turn off your phone and leave it in a bag of rice for 72 hours "
          "and after the waiting periodturn it on and it should work, if it "
          "doesn't your phone is broken, contact Apple to get it fixed"],

         ["Does your phone keep crashing? ",
          "Reset your phone and that should prevent your phone from crashing. "
          "If that doesn’t fix your problem contact Apple for more help."],

         ["Is your problem due to the recent update? ",
          "There could be multiple solutions to the this problem, "
          "go to 'www.apple.support/recentupdate.com and find your problem"],

          ]

def askit(question):
    retn = False

    ans = input(question[0])
    if ans == "yes":
        print(question[1])
        retn = True
    else:
        print("\nOkay let's move on to the next question")

    return retn

for q in q_and_a:
    if askit(q):
        break

如果函数中包含代码,则可以在“是”条件结束时使用
return
,以阻止其余代码执行


如果代码不在函数中,则可以使用
sys.exit()

这样可以跳过else子句并防止增加缩进

if q2 == "no":
def main():
    print("welcome to Apple online iPhone help system, I will ask you a series of questions and please answer with a 'yes' or 'no', if by the end our automatic troubleshooting system does not help you,there are more support and help options available on our website https://getsupport.apple.com/ ")

    q1 = input("Is your phone running slow? ")
    if q1 == "yes" :
        print("you need to free up space, phones slow down when they are full, try deleting unused apps or unnecessary photos or buy more storage. If that doesn’t fix your problem contact Apple for more help.")
        return

    print("okay let's move on to the next question")
    q2 = input("have your dropped your phone in water? ")
    if q2 == "yes":
        print("turn off your phone and leave your phone in a bag of rice for 72 hours and after the waiting periodturn it on and it should work, if it doesn't your phone is broken, contact apple to get it fixed")
        return

    print("okay let's move on to the next question")
    q3 = input("Does your phone keep crashing? ")
    if q3 == "yes":
    print("Reset your phone and that should prevent your phone from crashing, If that doesn’t fix your problem contact Apple for more help.")
        return

    print("okay let's move on to the next question")
    q4 = input("is your problem due to the recent update? ")
    if q4 == "yes":
        print("there could be multiple solutions to the this problem, go to 'www.apple.support/recentupdate.com and find your problem")
        return         

    print("okay let's move on to the next question")
    # the rest of your questions/answers


if __name__ == '__main__':
    main()

使用elif,不要得到错误的答案欢迎使用编程Maryam!你的代码让我想起了一个月前那只猫拖进来的东西,尽管这次我的眼睛里可能有血。我真的建议将问题和答案存储在一个文件中(每个问题3行:问题,回答如果是,回答如果否)。然后,您可以编写一个循环,读取文件(每次3行),打印相应的循环,并在需要时跳转到下一个问题。如果答案是“是”,则可以
返回
。这样就不会执行下一个代码,因此实际上不需要else,并且可以节省大量缩进。@SembeiNorimaki:我们不知道这是在函数中还是独立的。通常认为返回不止一个是错误的做法。在独立代码中,可以使用exit。我不同意这总是不好的做法。在这种情况下,代码缩进更少,更清晰。有时提前返回可以避免将条件带到函数末尾。@SembeiNorimaki:好的,我们将不得不在这一点上有分歧,我认为这是意大利面代码。我想你是指
sys.exit()
,因为脚本中不应该使用
exit()
。“return”是一个外部函数。我该如何解决这个问题@sembeinorimaki如果代码不在函数中,请使用
sys.exit()
而不是return(首先必须
导入sys
)。另一种选择是将代码放入函数中并使用return。我编辑了我的代码以使用一个可以调用的函数。