2013-07-14 60 views
-3

所以下面是我的代碼,我應該創建一個包含應該有距離的地方的列表。用戶應該選擇兩個不同的地方來查看它們以英里爲單位的距離,然後他們可以選擇轉換成公里。不過,我完成了代碼,所以它的工作,然後我去添加一個if語句,應該說像「如果用戶輸入place1,place2或地方3,然後繼續要求用戶進入第二個地方,否則重新啓動程序回到起點,但if語句不正確寫入,它現在說「二是沒有定義」爲什麼我的if語句意外?

print 'The available places to select from are:' 

print 'place1, place2, place3: ' 


place1 = 50 
place2 = 40 
place3 = 30 
Convert = 1.609344 



First = str(raw_input('Please select from the list where you are coming from')) 
if raw_input == 'place1' 'place2' 'place3': 
    Second = str(raw_input('Please select where you are going to: ')) 
else: 
    print 'please press any key to restart' 

Distance = First + Second 

print "the distance between the two places are", Distance, "miles" 



Kilometres = bool(input('Would you like to convert to Kilometres? please type True to convert or False to exit: ')) 
if True: 
     print 'The distance in Kilometres is',First + Second/Convert, 'Kilometres' 

else: 
     print 'press any ket to terminate' 
+5

是不是錯誤不言自明?它說你在'if'語句中的縮進是不正確的。您應該將if-else塊左移4個空格。 –

+1

縮進非常重要 - 您的'if' /'else'不能縮進 –

+0

另一個錯誤是您在設置First和Second之前定義了「Distance」。 –

回答

7

不縮進你的if語句。

First = int(input('Please select from the list where you are coming from')) 
if answer in ['place1', 'place2', 'place3']: 
    Second = int(input('Please select where you are going to: ')) 
else: 
    print 'please press any key to restart' 

,而不是

First = int(input('Please select from the list where you are coming from')) 
    if answer in ['place1', 'place2', 'place3']: 
     Second = int(input('Please select where you are going to: ')) 
    else: 
     print 'please press any key to restart' 

編輯:

看來你需要一些幫助。我重新編寫了代碼並添加了一些評論,希望能夠清理一些問題。

#using a dictionary allows us to associate, or hash, a string with a value 
place_dict = {"place1":50,"place2":40,"place3":30} 

convert = 1.609344 

def run_main(): 
    #placing the bulk of the program inside of a function allows for easy restarting 
    print 'The available places to select from are:' 
    print 'place1, place2, place3: ' 

    first = raw_input('Please select from the list where you are coming from: ') 
    second = raw_input('Please select where you are going: ') 
    #the values the user puts in are now stored in variables called 'first' and 'second', so if the user inputs "one", then first == "one" 

    if first not in place_dict or second not in place_dict: 
     #check to ensure that both first and second are in the place place dictionary 
     #if not, then return none to the main program 
     raw_input('Press any key to restart...') 
     return None 

    #an else is not needed here, because if the if statement executes, then we will not reach this point in the program 

    #this returns the dictionary value of the first value that the user inputs + the second. 
    return place_dict[first] + place_dict[second] 


if __name__ == "__main__": 
    #this line says that if we are running the program, then to execute. This is to guard against unwanted behavior when importing 

    distance = None 
    #set an initial variable to None 

    while distance is None: 
     #run the main program until we get an actual value for distance 
     distance = run_main() 

    print "the distance between the two places are", distance, "miles" 


    kilometres = bool(input('Would you like to convert to Kilometres? please type True to convert or False to exit: ')) 
    if kilometres == True: 
     print 'The distance in Kilometres is',distance/convert, 'Kilometres' 

    else: 
     print 'press any key to terminate'  
+0

謝謝你的回覆,我確實嘗試了這個,但是好像當我這樣做時if語句沒有正常工作。例如,我刪除然後indentations,然後故意鍵入除place1或place2或place3以外的東西,而不是輸出else語句,它只是崩潰和輸出place5沒有定義。我知道它沒有被定義,但它應該打印我的其他語句中的內容。有任何想法嗎? – Daniel

+0

有幾件事......現在,你是其他語句不重新啓動程序。發生什麼事是你擊中else語句,然後代碼繼續執行,它不會重新啓動。你可以使用一個函數來解決這個問題。 其次,你存儲的價值是作爲第一,但然後訪問是作爲答案(如果答案在...應該是如果先進)。 第三,你將你的輸入轉換爲一個int,然後將它與字符串進行比較。你不需要施放它。 – Schiem

+0

對不起Schiem讓我感到困惑,我可以理解else語句不會重新啓動程序,但至於問題二,我不太清楚如何糾正這個問題:/是的,至於第三個是我所有的我可以想到這樣做,我知道它剛纔顯示的時候,他們列出了一個包含地名的數字,代表距離,但我不記得他們是如何做到的,所以我的頭頂&試圖使用幾個不同的來源我找不到一個例子,其中有人用與字面相關的字符串創建一個列表。 – Daniel