如何将输入链接到Python中的新问题(故障排除程序)?

时间:2016-10-01 14:10:19

标签: python

我在python上编写了一个故障排除程序,我发现很难将输入链接到新问题:

   Question 1
   print("Has your car got a flat tyre? 1. Yes 2. No")
   choice=input("1/2")
   if choice == "1":
        goto (Question 2) #How do I link this input
   elif choice == "2":
        goto (Question 3)
   else:
        print("Answer not applicable")
   Question 2                 #Into this question?
   print("Have you taken your car to the petrol station? 1. Yes 2. No")
   choice=input("1/2")
   if choice == "1":
       goto (Question 4)
   elif choice == "2":
       goto (Question 5)
   else:
       print("Answer not applicable")
   Question 3
   print("Has your car recently had an MOT? 1. Yes 2. No")
   choice=input("1/2")
   if choice == "1":
       goto (Question 6)
   elif choice == "2":
       goto (Question 7)
   else:
       print("Answer not applicable")

在进一步完成项目之前,我需要知道如何做到这一点。感谢所有帮助。

1 个答案:

答案 0 :(得分:0)

Python不支持标签和转到。你应该做的是类似下面的代码。

QUESTION_1 = "Has your car got a flat tyre? 1. Yes 2. No"
QUESTION_2 = "Have you taken your car to the petrol station? 1. Yes 2. No"
QUESTION_3 = "Has your car recently had an MOT? 1. Yes 2. No"
QUESTION_4 = "" 
QUESTION_5 = "" 
QUESTION_6 = ""
QUESTION_7 = "" 

def ask_question(question):
    print(question)
    answer = input()
    return answer

def check_answer(question, answer):
    if question == QUESTION_1 and answer == "1":
        question = QUESTION_2
    elif question == QUESTION_1 and answer == "2":
        question = QUESTION_3
    elif question == QUESTION_2 and answer == "1":
        question = QUESTION_4
    elif question == QUESTION_2 and answer == "2":
        question = QUESTION_5
    elif question == QUESTION_3 and answer == "1":
        question = QUESTION_6
    elif question == QUESTION_3 and answer == "2":
        question = QUESTION_7
    else:
        print("Answer not applicable")

    return question

question = QUESTION_1

while True:
    answer = ask_question(question)
    question = check_answer(question)