为什么我得到ELIF无效语法?

时间:2013-02-21 00:19:10

标签: python

我看不出以下代码有什么问题:

    elif choice == "2":
    while True:
        PhoneSearch = raw_input("What is their telephone number? : ")
        conn = sqlite3.connect('SADS.db')
        cur = conn.cursor()
        cur.execute("SELECT * FROM customers WHERE Telephone = (?)",(PhoneSearch,))
        row = cur.fetchone()
        if row:
            CustID = row[0]
            print "|------------------------------------------|"
            print "|Customer ID : " , row[0]
            print "|Forename : " , row[1]
            print "|Surname : " , row[2]
            print "|Address Line 1 : " , row[3]
            print "|Address Line 2 : " , row[4]
            print "|City : " , row[5]
            print "|Postcode : " , row[6]
            print "|Telephone number : " , row[7]
            print "|E-Mail : " , row[8]
            while True:
                print '|Do you want to see what seats', row[1]
                choice = raw_input("|Choice Y/N:")
                if choice == 'Y':
                    cur.execute("SELECT * FROM seats WHERE CustID = (?)", (CustID,))
                    rowseat = cur.fetchone()
                    if rowseat: # or if rowseat is not None, etc.
                        print "|Seats booked:" , rowseat[0]
                        print "|------------------------------------------|"
                        break
                    else:
                        print("database doesn't have correct info")
            else:
                print("Na")

然而,我在顶部的Elif语句中遇到语法错误。请告诉我为什么会发生这种情况或错误发生的地方?

2 个答案:

答案 0 :(得分:3)

Python期望在elif语句后加上缩进块,与ifwhileelse语句相同。在您的示例中,elif之后的所有内容都应缩进。

答案 1 :(得分:0)

缩进elif下的所有内容,如果elif正确,请将其包含在要执行的操作中。 目前它意味着:

elif choice == "2":
     pass  # nothing here
while True:
     PhoneSearch = raw_input("What is their telephone number? : ")
     # etc

所以如果它是正确的,它就找不到任何事情,无论如何都要做。如果这是你想要做的,你可以把“通过”(减去“”)放在## nothing here