简体   繁体   中英

Why does the following code produce an indentation error?

def search():
    try:
        option=input("\n\nWhta do you want to search by ('A' for account type, 'B' for balance): ")
        if option.lower()=='a':
            option_2=input("\n\nWhat type of account do you want to view ('C' for current,'S' for savings): ")
            if option_2.upper()=="C":
                inFile=open("account.dat","rb")
                acc_det=pickle.load(inFile)
                for x in acc_det:
                    if x.rettype()=="C":
                            print("\n\n\tACCOUNT HOLDER LIST\n\n")
                            print(60*"=")
                            print("%-10s"%"A/C No.","%-20s"%"Name","%-10s"%"Type","%-6s"%"Balance")
                            print(60*"=","\n")
                            x.report()

    except EOFError:
        print("Enter Valid Statement")



"""*****************************************************************************
                        THE MAIN FUNCTION OF PROGRAM
*****************************************************************************"""

intro()

while True:
    print(3*"\n",60*"=")
    print("""MAIN MENU

    1. New Account
    2. Deposit Amount
    3. Withdraw Amount
    4. Balance Enquiry
    5. All Account Holder List
    6. Close An Account
    7. Modify An Account
    8. Exit
    9. Filter Accounts
    """)

The code gives an indentation error right after the last triple quote. I can't figure out why, but the error goes away if I remove the "try" clause. Why is this happening?

Edit: I have edited in the next part of the code, where I call in the main function

In python, after every ":" and newline, there needs to be an indentation of atleast one space or tab character.

In your above program, you have declared a function search() , and have a semi-colon after it, so in the next line, you need to indent the statements inside the function.

So, you will have to indent try: and except: statements, and recursively keep indenting the code blocks present in the try/except clauses

I can see two critical section in regards of indentation:

  1. try: and except EOFError: have the same indentaion as def search():
  2. Your print statements are indented by 8 spaces

maybe here is a problem:

def search():
try:

try this:

def search():
    try:

do def search() and try: have same level of indentation?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM