-5

这段代码:

def viewdetails():
    os.system('cls')
    print "|__________________________________________|"
    print "|----------------View Details--------------|"
    print "|------------------------------------------|"
    print "|1 - Search with Customer ID               |"
    print "|2 - Search with Telephone number          |"
    print "|------------------------------------------|"
    choice = raw_input("|Choice: ")
    if choice == "1":
        while True:
            IDSearch = raw_input("|What is their ID? : ")
            conn = sqlite3.connect('SADS.db')
            cur = conn.cursor()
            cur.execute("SELECT * FROM customers WHERE CustID = (?)",(IDSearch,))
            row = cur.fetchone()
            if row[0] != IDSearch:
                print "|------------------------------------------|"
                print "|          Invalid ID, Try again!          |"
                print "|------------------------------------------|"
            else:
                break
        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], 'has booked?|'
            choice = raw_input("|Y/N: ")
            if choice == 'Y':
                cur.execute("SELECT * FROM seats WHERE CustID = (?)", (CustID,))
                rowseat = cur.fetchone()
                if rowseat:
                    print "|Seats booked:" , rowseat[0]
                    print "|------------------------------------------|"
                    break
                else:
                    print "|" , row[1] , "Hasnt booked any seats."
                    break
    print(" ")
    print "|------------------------------------------|"
    print("|Please select an option:                  |")
    print("|1 - Return to menu                        |")
    print("|2 - Book seats                            |")
    print("|3 - Edit details                          |")
    choice = raw_input("|Please put choice here : ")
    print "|------------------------------------------|"
    elif choice == "2":
        while True:
            IDSearch = raw_input("|What is their ID? : ")
            conn = sqlite3.connect('SADS.db')
            cur = conn.cursor()
            cur.execute("SELECT * FROM customers WHERE CustID = (?)",(IDSearch,))
            row = cur.fetchone()
            if row[0] != IDSearch:
                print "|------------------------------------------|"
                print "|          Invalid ID, Try again!          |"
                print "|------------------------------------------|"
            else:
                break
        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], 'has booked?|'
            choice = raw_input("|Y/N: ")
            if choice == 'Y':
                cur.execute("SELECT * FROM seats WHERE CustID = (?)", (CustID,))
                rowseat = cur.fetchone()
                if rowseat:
                    print "|Seats booked:" , rowseat[0]
                    print "|------------------------------------------|"
                    break
                else:
                    print "|" , row[1] , "Hasnt booked any seats."
                    break
    print(" ")
    print "|------------------------------------------|"
    print("|Please select an option:                  |")
    print("|1 - Return to menu                        |")
    print("|2 - Book seats                            |")
    print("|3 - Edit details                          |")
    choice = raw_input("|Please put choice here : ")
    print "|------------------------------------------|"
    print(" ")
    print "|------------------------------------------|"
    print("|Please select an option:                  |")
    print("|1 - Return to menu                        |")
    print("|2 - Book seats                            |")
    print("|3 - Edit details                          |")
    choice = raw_input("|Please put choice here : ")
    print "|------------------------------------------|"
    if choice == "1":
        mainprogram()
    elif choice == "2":
        availablity()
    elif choice == "3":
        editdetails()
    os.system('cls')

viewdetails()

( http://pastebin.com/X62jRLiL )

获取 elif 语句elif choice == "2":显然是一种无效的语法,但elif choice == "1":作品和下面的代码是彼此的精确复制品吗?

4

2 回答 2

3

Python 代码需要正确缩进。

否则无效。

这可能是您了解 Python 的第一件事(所以请拿起一本书,例如“Thinking in Python”);同时,最受批评的事情(至少是非用户)。

有效代码示例:

if a == "1":
    echo "Apples"
elif b == "2":
    echo "Bananas"

由于缩进错误导致的无效代码:

if a == "1":
echo "Apples"
elif b == "2":
    echo "Bananas"
于 2013-02-21T23:04:47.240 回答
1

您正在混合制表符和空格,不要那样做。使用以下方式运行脚本:

python -tt yourscript.py

去哪里找。将所有制表符替换为空格,将编辑器配置为仅使用空格进行缩进。

于 2013-02-21T23:09:16.163 回答