Elif无效?

2024-09-27 19:30:37 发布

您现在位置:Python中文网/ 问答频道 /正文

此代码:

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":起作用了,下面的代码是另一个的完全复制品吗?在


Tags: idinputrawifrowprintpleasebreak
2条回答

你在混合制表符和空格,不要这样做。以以下身份运行脚本:

python -tt yourscript.py

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

Python代码需要正确缩进。在

否则无效。在

这可能是您了解python的第一件事(所以请拿起一本书,例如《用python思考》);同时,也是最具批判性的东西(至少非用户)。在

有效代码示例:

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

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

^{pr2}$

相关问题 更多 >

    热门问题