2017-03-27 163 views
-6

我if語句有這個問題這段代碼爲什麼會產生語法錯誤?

while True: 
    try: 
     continue_shopping=int(input("press 0 to stop shopping and print your reciept or press 1 to continue shopping")) 
    if continue_shopping !=0 and continue_shopping !=1: 
     print(" make sure you enter 0 or 1") 
    if continue_shopping !=1 
    keep_searching = False 
    if (continue_shopping == 0): 
     fileOne.close() 
     fileTwo = open("receipt.csv" , "r") 
     reader = csv.reader(fileTwo) 
     for row in reader: 
      if row != None: 
       print(row) 
    elif continue_shopping==1: 
     search_user_input() 
     quantity() 
quantity() 

第五行產生一個無效的語法錯誤「如果」以紅色突出顯示,我不確定這是爲什麼

+1

你忘了「:」在條件 – N0un

+1

結束時,您不能使用'試穿if'條款 –

+0

旁邊的語法錯誤:Python的需要適當的縮進。 try:塊中的所有代碼都需要一個idont indenation。 keep_searching = False。 – RvdK

回答

1

你忘了的情況下「:」在條件結束時。

if continue_shopping !=1: 

編輯 - 第二個問題:當然,你仍然有其他語法錯誤。之後你try你應該有一個except條款。在問這樣的問題之前,你真的必須讀documentation

+0

仍然獲得第五行-_-一個無效的語法 – MeTooThanks

+0

所以這應該是罰款?而真: 嘗試: continue_shopping = INT(輸入( 「請按0停止購物並打印收據或按1繼續購物」)) 如果continue_shopping = 0和continue_shopping = 1: 除了 打印(」請確保您輸入0或1「) if continue_shopping!= 1: keep_searching = False if(continue_shopping == 0): fileOne.close() – MeTooThanks

+1

如何在此註釋中讀取縮進? – N0un

0

下一步在if continue_shopping !=1:末語法eror分號(中指出@ N0un

  • 解決您的壓痕,沒有嘗試,如果塊。你應該縮進如果條件
  • 返修您的IFS :他們砸鍋就錯誤情況
  • 除了你嘗試

例如漏判:(沒有一個Python編譯器在這裏,所以裸語法錯誤:)

while True: 
    try: 
     continue_shopping=int(input("press 0 to stop shopping and print your reciept or press 1 to continue shopping")) 
     if continue_shopping !=0 and continue_shopping !=1: 
      print(" make sure you enter 0 or 1") 
      continue 

     if (continue_shopping == 0): 
      fileOne.close() 
      fileTwo = open("receipt.csv" , "r") 
      reader = csv.reader(fileTwo) 
      for row in reader: 
       if row != None: 
        print(row) 
     else: 
      search_user_input() 
      quantity() 
    except: 
     print("whups some exception") 
相關問題