简体   繁体   English

为什么下面的代码会产生缩进错误?

[英]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.我不知道为什么,但是如果我删除“try”子句,错误就会消失。 Why is this happening?为什么会这样?

Edit: I have edited in the next part of the code, where I call in the main function编辑:我在代码的下一部分进行了编辑,我在其中调用了主 function

In python, after every ":" and newline, there needs to be an indentation of atleast one space or tab character.在 python 中,在每个“:”和换行符之后,至少需要有一个空格或制表符的缩进。

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.在上面的程序中,您已经声明了一个 function search() ,并且后面有一个分号,因此在下一行中,您需要缩进 function 中的语句。

So, you will have to indent try: and except: statements, and recursively keep indenting the code blocks present in the try/except clauses因此,您必须缩进try:except:语句,并递归地缩进 try/except 子句中的代码块

I can see two critical section in regards of indentation:我可以看到关于缩进的两个关键部分:

  1. try: and except EOFError: have the same indentaion as def search(): try:并且except EOFError:具有与def search():
  2. Your print statements are indented by 8 spaces您的print语句缩进 8 个空格

maybe here is a problem:也许这是一个问题:

def search():
try:

try this:尝试这个:

def search():
    try:

do def search() and try: have same level of indentation?执行 def search() 并尝试:具有相同级别的缩进?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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