2013-02-21 43 views
0

我看不到什么毛病下面的代码:为什么我会收到ELIF无效的语法?

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") 

但我得到顶部的艾利芙语句的语法错误。请告诉我为什么会发生这种情况或错误是在哪里?

+4

你的代码实际上是这样缩进的('elif'与'while ')?如果是这样,那是一个问题。另外,在这之前你有一个'if',而不是只以'elif'开始,对吧? – Dougal 2013-02-21 00:21:47

回答

3

Python在您的elif声明后期望有一个缩进块,与if,whileelse语句相同。在你的例子中,elif之后的所有内容都应该缩进。

+0

一切都排队了,事实上if语句上面是if ==“1”是这个的精确副本,但它仍然给出一个无效的语法 – OcelotcR 2013-02-21 18:58:04

+0

在这种情况下,你确定你没有犯错在你的脚本早些时候(例如函数调用后没有关闭括号)? – 2013-02-27 00:45:34

0

缩进elif下的所有东西,将它包含在要做的事情中,如果elif是正确的。 目前,它的意思是:

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

所以也没有找到什么做的,如果它是正确的,反正会做一段时间。如果这是你想要做的,你可以在##的地方加上“pass”(减去“”),这里没有任何东西

相关问题