2017-03-08 84 views
-1

編輯:這個問題是在我的Python學習過程的開始問。 Syntax Error由pythons IDLE生產,沒有引用說明。當人們要求完整的錯誤時,這是​​造成問題和困惑的主要原因。while循環結束時的語法錯誤

我正在做一個簡單的筆記回憶程序。我不是100%確定爲什麼如果有人能夠協助,我會不斷收到語法錯誤。

注意:錯誤只是「語法錯誤」。沒有其他信息顯示該錯誤。

錯誤顯示在program = False所在的程序代碼末尾。我不允許在印刷或其他東西后放置它嗎?

請記住,我對Python和編程一般都很陌生。所以如果你有解決方案,請解釋我做錯了什麼。

#################################################################################### 
''' Goal = quick access list of notes that I can add to or remove from as needed.''' 
''' Note: this script is designed for python 3.2+ check converted elements ''' 
#################################################################################### 

notes = { 
    'ban': 'BAN: is the account number.', 
    'bdt': 'testing derp' 
    } 

program = True 
active = False 

def note_finder(word): 

    while active == True: 
     print ('Type one of the following keywords','\n','ban','\n','test','\n','test2','\n', 'Or type exit to close') 
     choice2 = input('--> ').lower() 
     if choice2 == 'exit': 
      print ('Exiting Function') 
      active = False 
      program = True 
     elif choice2 in notes: 
     print (notes[choice2]) 
     else: 
     print ("Not a Keyword") 

while program == True: 
    print ('Type one of the following options:','\n','1','\n','2','\n','3') 
    choice1 = int(input('--> ')) 
    if choice1 < 1 or choice1 > 3: 
     print ("Not an option") 
    else: 
     print (note_finder(input('--->')) 
     program = False 
     active = True 
+2

上一行有不匹配的圓括號。 – ForceBru

+0

不確定爲什麼這個問題被拒絕投票。這是一個合理的問題。不像我要求某人爲我寫任何代碼。 –

+0

只是FYI,包括問題中的* full *語法錯誤消息將幫助我們更輕鬆地幫助您!另外,錯誤信息甚至可以幫助你解決問題。 –

回答

1

您在打印行末尾缺少圓括號。

您有:

print (note_finder(input('--->')) 

應該是:

else: 
    print (note_finder(input('--->'))) 
    program = False 
    active = True 
1

由於沒有錯誤代碼給,我可以清楚地看到一個錯誤:

while program == True: 
    print ('Type one of the following options:','\n','1','\n','2','\n','3') 
    choice1 = int(input('--> ')) 
    if choice1 < 1 or choice1 > 3: 
     print ("Not an option") 
    else: 
     print (note_finder(input('--->')) # mismatched parentheses(add a ')') 
     program = False 
     active = True 
0

這將有助於如果你告訴我們錯誤是什麼。要查看錯誤是什麼,最簡單的方法是以交互模式運行程序。它會告訴你:

File "...", line 19 
    print (notes[choice2]) 
     ^
IndentationError: expected an indented block 

這很清楚。這意味着這行代碼應該比之前的代碼縮進更多,但事實並非如此。

在Python中的每個冒號:之後,您需要一個縮進塊。例如

elif choice2 in notes: 
print (notes[choice2]) 

應該

elif choice2 in notes: 
    print (notes[choice2]) 
0

原來的問題與語法錯誤,是因爲在我的打印語句缺少「)」中。

感謝'Karan Nagpal'&'Agile Jedi'爲您的快速反應。

但是之後,我遇到了一些其他問題。

我已更正了其他問題,並稍微更改了一些代碼,以完成我正在嘗試執行的操作,而沒有發生任何問題。

這是新的工作代碼,如果任何人有興趣。

#################################################################################### 
''' Goal = quick access list of notes that I can add to or remove from as needed.''' 
''' Note: this script is designed for python 3.2+ check converted elements ''' 
#################################################################################### 

notes = { 
    'ban': 'BAN: is the account number.', 
    'bdt': 'testing derp' 
    } 

switch = True 

def note_finder(word): 
     print ('Type one of the following keywords','\n','ban','\n','test','\n','test2','\n', 'Or type exit to close') 
     choice2 = input('--> ').lower() 
     if choice2 == 'exit': 
      print ('Exiting Function') 
      switch = True 
     elif choice2 in notes: 
      print (notes[choice2]) 
     else: 
      print ("Not a Keyword") 

while switch == True: 
    print ('Type one of the following options:','\n','1','\n','No other options at this time.') 
    choice1 = int(input('--> ')) 
    if choice1 < 1 or choice1 > 1: 
     print ("Not an option") 
    else: 
     switch = False 
     note_finder(input)