Syntax Error at the end of a while loop

删除回忆录丶 提交于 2019-12-25 18:26:15

问题


EDIT: This question was ask at the start of my learning process for python. The Syntax Error was produced by pythons IDLE with no trackback to speak of. This was the main cause of the problem and confusion when people asked for the full error.

I am working on a simple note recall program. I am not 100% sure why I keep getting a syntax error if anyone can assist.

Note: The error was only "Syntax Error". There was no other information displayed with the error.

The error is showing up at the end of the program code where program = False is. Am I not allowed to put that after print or something?

Keep in mind I am very new to Python and programming in general. So if you do have a solution please explain what I was doing wrong.

####################################################################################
''' 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

回答1:


You're missing a parenthesis at the end of the print line.

YOU HAVE:

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

IT SHOULD BE:

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



回答2:


As no error code is given, I can see one error clearly :

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



回答3:


It would help if you told us what the error is. To see what the error is, the easiest way is to run your program in interactive mode. It will tell you:

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

This is pretty clear. It means that that line of code should be indented more than the one that came before, but it isn't.

After every colon : in Python you need an indented block. For example

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

should be

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



回答4:


The original problem with the syntax error was because of the missing ")" on my print statement.

Thanks to 'Karan Nagpal' & 'Agile Jedi' for your quick response.

However after that was fixed I ran into some other issues.

I have corrected the other issues and changed the code up a little to do exactly what I was trying to do without any problems.

Here is the new working code if anyone is interested.

####################################################################################
''' 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)


来源:https://stackoverflow.com/questions/42677507/syntax-error-at-the-end-of-a-while-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!