“Unexpected EOF while parsing” after a “try” statement

青春壹個敷衍的年華 提交于 2019-12-20 06:39:54

问题


I'm really, really new to Python and was making a small test program.

Here is my code:

def start ():   
    print ("This is where text would be")   
    prompt_sta ()

def prompt_sta ():  
    prompt_0=raw_input("Input a Command: ")  
    try:  
        if prompt_0 == 'Okay':                             
           next_screen ()  
        else:   
            print ('Type Okay.')   
            prompt_sta ()   

when I try to run it I get the "Unexpected EOF while parsing" error.


回答1:


For the EOF error, you can just get rid of that try: like so

def start ():   
    print ("This is where text would be")   
    prompt_sta ()

def prompt_sta ():  
    prompt_0=raw_input("Input a Command: ")  

    if prompt_0 == 'Okay':                             
        next_screen ()  
    else:   
        print ('Type Okay.')   
        prompt_sta() 

You can also just add an except clause, as Fernando said, if you still want to use try:




回答2:


The try needs an except clause.




回答3:


You need an except after the try. It's just a small syntax error; the eof comes from the python parser before it executes the code.

def start ():   
    print ("This is where text would be")   
    prompt_sta ()

def prompt_sta ():  
    prompt_0=raw_input("Input a Command: ")  
    try:  
        if prompt_0 == 'Okay':                             
           next_screen ()  
        else:   
            print ('Type Okay.')   
            prompt_sta ()  
    except Exception as ex:
        print (ex)

Here's the link to docs just to save a quick google: https://docs.python.org/3/tutorial/errors.html



来源:https://stackoverflow.com/questions/28007727/unexpected-eof-while-parsing-after-a-try-statement

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