简体   繁体   English

Python:1个函数能否接受来自2个不同功能的变量

[英]Python: Can 1 function accept variables from 2 different fucntions

OK - I am trying to get a Python function to accept variables from two other functions. OK-我正在尝试获取一个Python函数以接受来自其他两个函数的变量。 Is this possible ? 这可能吗 ?

A sample of what I am trying to do it below (I have simmed down the original code - for input here). 下面是我尝试做的一个示例(我已经简化了原始代码-在这里输入)。 Hopefully you get theidea of what I am trying to do. 希望您能理解我正在尝试做的事情。 In a nutshell, I have Rectangle () which calls Extras() and the I want the output from Rectangle and Extras to be sent to the Calculate_Deposit (). 简而言之,我拥有调用Extras()的Rectangle(),并且我希望将Rectangle和Extras的输出发送到Calculate_Deposit()。

Is this possible ? 这可能吗 ?

def calculate_deposit(total_cost, extras):
    deposit_percent = float(raw_input("Enter Deposit % (as a decimal) of Total Cost:    "))
    months_duration = float(raw_input("Enter the number of months client requires:    "))
    if deposit_percent >0:
        IN HERE JUST SOME CALCULATIONS
    else:
        print "The total amount required is:     ", total_cost

def rectangle(width, height, depth, thickness):
    type = raw_input("Enter lowercase c for concrete:  ")
    if type == 'c':
        output = IN HERE JUST COME CALCULATIONS
    else:
        return raw_input("Oops!, something went wrong")     
    print output + extras()
    total_cost = calculate_deposit(output, extras)                          

def extras():
    type = float(raw_input("Enter 1 for lights:  "))
    if type == 1:
        light = 200
        print "The cost of lights are:    ", light
        return light
    else:
        return raw_input("No extras entered")

In rectangle , you call extras() , then you send just the function extras to calculate_deposit() . rectangle ,您调用extras() ,然后仅将函数extras发送到calculate_deposit() You want to send the result of the extras() call, not a reference to the function itself. 您要发送extras()调用的结果,而不是对函数本身的引用。 You can make a minor change and save that value, referring to it when you print and when you go into calculate_deposit . 您可以进行较小的更改并保存该值,在打印时以及进入calculate_deposit时都引用它。

Change this: 更改此:

print output + extras()
total_cost = calculate_deposit(output, extras)

To this: 对此:

extra = extras()
print output + extra
total_cost = calculate_deposit(output, extra)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM