2016-10-01 48 views
0

我編程的蟒蛇疑難解答,我一直在尋找它真的很難鏈路輸入了新的問題:如何將輸入鏈接到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

你不能這樣做,這是沒有意義的。 Python沒有GOTO,你不能跳轉到某個標記。考慮使用函數來表示問題,然後調用適當的函數。 – jonrsharpe

回答

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

這是一種不雅的做事方式 - 爲什麼不列出問題或詞典? – jonrsharpe

+0

我同意這不是很好,但用戶顯然沒有太多的Python知識,所以我實現了一個沒有數據結構的解決方案,因爲他可能沒有這些經驗。 –