python - 在Python中创建多个需要能够互相调用的函数

标签 python arguments function

所以首先我要说的是,我是 Python 的新手,目前函数似乎超出了我的理解范围,但我遇到的问题是让 3 个函数能够相互调用。这是我的代码(是的,我知道这是非常错误的,但你应该看看我要去哪里):

def menu():
    count=gearboxes
    cost=subtotal
    return subtotal


def quantity():

    gearboxes=raw_input("How many gearboxes would you like to order? ")

    return menu()



def subtotal(cost):
    if (gearboxes<=10):
        cost=gearboxes*100.0
        print cost
    elif (gearboxes>10 and gearboxes<20):
        cost=(gearboxes-10)*80.0+1000.0
        print cost
    elif (gearboxes>20):
        cost=(gearboxes-20)*70.0+1000.0+800.0
        print cost
    else:
        print "wtf m8"

    return menu()

def summary():
    print "="*80
    print "%60s %20f %20f" % ("motors",count,cost)
    print "="*80


print quantity()
print subtotal(menu)
print summary(menu)

就是这样,如果您能解释一下函数如何相互调用,我们将不胜感激。

谢谢!

修复版本(仍然有效)

    def quantity():
        motors=raw_input("How many motors would you like to order? ")
        gearboxes=raw_input("How many gearboxes would you like to order? ")
        sensors=raw_input("How many sensor boards would you like to order? ")

        return int(motors),int(gearboxes),int(sensors)



   def subtotal(motors,gearboxes,sensors):

        if motors<=10 and gearboxes<=15:
            motorCost=motors*100
            gearboxCost=gearboxes*50
            sensorCost=sensors*66
            return motorCost, gearboxCost, sensorCost

        if motors>10 and motors<=20 and gearboxes>15 and gearboxes<=30:
            motorCost=(motors-10)*80+1000
            gearboxCost=(gearboxes-15)*40+750
            sensorCost=sensors*66
            return motorCost, gearboxCost, sensorCost

        elif motors>20 and gearboxes>30:
            motorCost=(motors-20)*70+1000+800
            gearboxCost=(gearboxes-30)*30+750+600
            sensorCost=sensors*66
            return motorCost, gearboxCost, sensorCost

    def summary(motors,gearboxes,sensors,motorCost,gearboxCost,sensorCost):
        print "="*80
        print "%60s %20d %20d" % ("motors",motors,motorCost)
        print "%60s %20d %20d" % ("gearboxes",gearboxes,gearboxCost)
        print "%60s %20d %20d" % ("sensor boards",sensors,sensorCost)
        print "="*80


   def menu():

        a,b,c=quantity()
        d,e,f=subtotal(a,b,c)
        summary(a,b,c,d,e,f)
        return


    menu()

最佳答案

我对您的代码做了一些更改。像对待问题一样对待函数。当你调用该函数时;你在问这个问题。您传递给 return 的是问题的答案。因此,当有人询问一些变速箱的小计时;我们返回cost,无论它是什么。

然后我们可以将返回值(答案)存储在变量中并在以后使用它们。例如,传递给另一个函数。尝试跟踪信息如何在程序中流动。

def quantity():
    count=raw_input("How many gearboxes would you like to order? ")
    return int(count)



def subtotal(count):
    if count<=10:
        cost=count*100.0
        return cost
    elif count>10 and count<20:
        cost=(count-10)*80.0+1000.0
        return cost
    elif count>20:
        cost=(count-20)*70.0+1000.0+800.0
        return cost

def summary(count, cost):
    print "="*80
    print "%60s %20f %20f" % ("motors",count,cost)
    print "="*80

def menu():
    items = quantity()
    sub = subtotal(items)
    summary(items, sub)

if __name__ == '__main__':
    menu()

关于python - 在Python中创建多个需要能够互相调用的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9355706/

相关文章:

function - 在lua中每次调用重用具有不同参数的协程

python - 函数如何接受冒号(范围运算符)作为参数(在 Python 中)?

arrays - 如何使用 scanf 将元素接收到数组中?

Javascript:使用不同的执行上下文执行函数

python - timedelta 不支持的类型

python - 有条件地将不同 DataFrame 中的聚合列连接到新的 DataFrame 中

python - 使用 Cython 优化 NumPy

java - 参数不会传递到对象中

function - 将多个参数传递给Powershell中的函数变量

python - 查找字典值中嵌套列表的列的平均值