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

前端 未结 3 1003
我寻月下人不归
我寻月下人不归 2021-01-27 20:38

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 b         


        
相关标签:
3条回答
  • 2021-01-27 21:05

    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

    0 讨论(0)
  • 2021-01-27 21:07

    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:

    0 讨论(0)
  • 2021-01-27 21:13

    The try needs an except clause.

    0 讨论(0)
提交回复
热议问题