Elif导致语法错误

时间:2014-12-02 06:15:22

标签: python python-3.x

我正在尝试编写一个函数(在Python 3.4中),允许用户选择将显示哪个输出。我的第一个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 个答案:

答案 0 :(得分: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")

答案 1 :(得分: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")