Elif无效?

时间:2013-02-21 23:03:12

标签: python sqlite

此代码:

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":是否有效,而后面的代码是彼此的完全复制品?

2 个答案:

答案 0 :(得分:3)

Python代码需要正确缩进。

否则,它无效。

这可能是你学习python的第一件事(所以要抓一本书,比如“用Python思考”);同时,最受批评的事情(至少是非用户)。

有效代码示例:

if a == "1":
    echo "Apples"
elif b == "2":
    echo "Bananas"
由于缩进错误

代码无效

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

答案 1 :(得分:1)

您正在混合制表符和空格,请勿这样做。使用as:

运行脚本
python -tt yourscript.py

找到哪里。用空格替换所有选项卡,将编辑器配置为仅使用空格进行缩进。