2014-12-02 166 views
-3

我正在尝试编写一个函数(在Python 3.4中),该函数将允许用户对将显示哪个输出做出选择。我的第一条elif线不断被标记为无效语法。我不确定我做错了什么。这里是我的代码:Elif导致语法错误

def questionFunction() : 
      q1=input('Would you like to: \n1. Submit Input and see costs \n2. View Summary Data \n3. Reset Summary Data \n4. Exit \n Input Corresponding Number Choice here: ') 

      if q1 == 1: 
        print ("Cost = $" + format(str(costFunction()), '.4')) 
        print ("Shipping Cost = $" + format(str(shippingFunction()), '.4')) 
        print ("Total Cost = $" + format(str(totalCostFunction()), '.4') 
      elif q1 == 2: 
        print ("User Count = " +str(count)) 
        print ("Total Revenue = $" + format(str(totalrevenue), '.4')) 
      elif q1 == 3: 
        count = 0 
        totalrevenue = 0 
      else 
        print ("This program will now close") 
+2

冒号在else子句中缺失 – xecgr 2014-12-02 06:16:18

+2

您错过了关闭''''。 4在你的第一个'elif'之前打开3关闭行。 – halex 2014-12-02 06:16:49

+0

谢谢!那是它造成的。 – NZS 2014-12-02 06:30:25

回答

0

请在最后else添加:

您的代码必须像

def questionFunction() : 
    q1=input('Would you like to: \n1. Submit Input and see costs \n2. View Summary Data \n3. Reset Summary Data \n4. Exit \n Input Corresponding Number Choice here: ') 

    if q1 == 1: 
     print ("Cost = $" + format(str(costFunction()), '.4')) 
     print ("Shipping Cost = $" + format(str(shippingFunction()), '.4')) 
     print ("Total Cost = $" + format(str(totalCostFunction()), '.4') 
    elif q1 == 2: 
     print ("User Count = " +str(count)) 
     print ("Total Revenue = $" + format(str(totalrevenue), '.4')) 
    elif q1 == 3: 
     count = 0 
     totalrevenue = 0 
    else: 
     print ("This program will now close") 
+0

谢谢你指出一个!但它仍然给我错误。 – NZS 2014-12-02 06:26:50

+0

您是否需要更新新的错误? – Nilesh 2014-12-02 06:27:25

+0

可能是你的代码有选项卡和空间混合。请复制上面的代码并让我知道错误。 – Nilesh 2014-12-02 06:28:46

1

这编译没有任何问题。

def questionFunction() : 
      q1=input('Would you like to: \n1. Submit Input and see costs \n2. View Summary Data \n3. Reset Summary Data \n4. Exit \n Input Corresponding Number Choice here: ') 

      if q1 == 1: 
        print ("Cost = $" + format(str(costFunction()), '.4')) 
        print ("Shipping Cost = $" + format(str(shippingFunction()), '.4')) 
        print ("Total Cost = $" + format(str(totalCostFunction()), '.4')) 
      elif q1 == 2: 
        print ("User Count = " +str(count)) 
        print ("Total Revenue = $" + format(str(totalrevenue), '.4')) 
      elif q1 == 3: 
        count = 0 
        totalrevenue = 0 
      else: 
        print ("This program will now close") 
+0

是的Dinesh,你从例子中复制出来,所以你有所有的空格,但是他的文件中有'tab'和'spaces'。 – Nilesh 2014-12-02 06:38:16

+0

是的,这是另一个弹出的问题。虽然很简单,但可以修复。 – NZS 2014-12-02 06:49:32

+0

我已经使用这个[链接](http://pythoniter.appspot.com/),它适当地格式化您的Python代码。 – 2014-12-02 07:26:34