简体   繁体   English

无法将用户定义函数的输入传递给其他函数,并对返回的值求和

[英]Trouble passing inputs of User-Defined function to other functions and summing returned values

I'm a N00b learning Python. 我是学习Python的N00b。 As part of a small project to create a simple calculator I need a function to take two inputs and determine total costs for a trip. 作为创建一个简单计算器的小项目的一部分,我需要一个函数来接受两个输入并确定一次旅行的总费用。

At the bottom of this code, I have a function called "trip_cost()" that takes two inputs, city and days. 在此代码的底部,我有一个名为“ trip_cost()”的函数,它需要两个输入,即city和days。 City is a string of the city you are going to visit and days is an integer value of days you are staying. “城市”是您要访问的城市的字符串,“天”是您要停留的天数的整数。

trip_cost() pass the inputs to rental_car_cost, hotel_cost, and plane_ride_cost functions and return a sum of their respective outputs. trip_cost()将输入传递给rental_car_cost,hotel_cost和plane_ride_cost函数,并返回它们各自输出的总和。

I understand that a function can be called from another function as a variable by name but I'm confused as to how I should process the input from trip_cost() into the other functions and return three values to be summed within trip_cost(). 我知道一个函数可以按名称作为变量从另一个函数调用,但是我对应该如何将trip_cost()的输入处理到其他函数中并返回三个要在trip_cost()中求和的值感到困惑。

Any advice would be greatly appreciated. 任何建议将不胜感激。

Thanks, 谢谢,

-- Matt -马特

def hotel_cost(n):
    nights = n * 140
    return nights

hotel_cost(3)

def plane_ride_cost(c):
    if c =="Charlotte":
        return 183
else:
    if c == "Tampa":
        return 220
    else:
        if c == "Pittsburgh":
            return 222
        else:
            if c == "Los Angeles":
                return 475

plane_ride_cost("Tampa")

def rental_car_cost(d):
    days = 40
    if d >= 7:
    return d * days - 50
elif d >= 3:
    return d * days - 20
else:
    return d * days

rental_car_cost(3)

def trip_cost(c,d):
    return sum (hotel_cost) + sum(rental_car_cost) + sum (plane_ride_cost)

trip_cost("Tampa",3)

I've created a "better" version of yours, sice you are learning python, I've put a lot of things here: 我创建了您的“更好”版本,因为您正在学习python,我在这里做了很多事情:

def plane_ride_cost(city):  # First, in this case, I used a dictionary
    return {                # to get a value of a city
        'Charlotte': 183,
        'Tampa': 220,
        'Pittsburgh': 222,
        'Los Angeles': 475
    }[city]

def hotel_cost(nights):     # You can always use expressions in
    return nights * 140     # the return statement

def rental_car_cost(days):  # If it is possible, use long variable
    cost = 40               # names, not just one letters -> readability counts
    if days >= 7:
        return days * cost - 50
    elif days >= 3:
        return days * cost - 20
    else:
        return days * cost

def trip_cost(city, days):  # You can just add the values together, that is SUM
    return hotel_cost(days) + rental_car_cost(days) + plane_ride_cost(city)

Demo: 演示:

print trip_cost('Tampa', 3) # Call the 'main' function, with variables

And this will return: 这将返回:

# 740

You don't really need sum() here because the following would work and require little overhead: 您在这里实际上并不需要sum() ,因为以下代码可以工作并且需要很少的开销:

def trip_cost(c, d):
    return hotel_cost(d) + rental_car_cost(d) + plane_ride_cost(c)

However, you could use sum() with something like the following, which creates a tuple of the results of calling each cost function, and then pass that object to sum() to be totaled. 但是,您可以sum()与类似以下内容的东西一起使用,这将创建调用每个cost函数的结果的元组,然后将该对象传递给sum()进行总计。

def trip_cost(c, d):
    return sum((hotel_cost(d), rental_car_cost(d), plane_ride_cost(c)))

Another alternative would be to use a generator expression , but it would require each of the functions to accept the same input arguments even if they didn't use both of them. 另一个选择是使用生成器表达式 ,但是它将要求每个函数都接受相同的输入参数,即使它们没有使用它们。 If they were rewritten to be that way, it would allow you to do something along these lines: 如果以这种方式重写它们,则可以按照以下方式进行操作:

def trip_cost(c, d):
    cost_functions = (hotel_cost, rental_car_cost, plane_ride_cost)
    return sum(cf(c, d) for cf in cost_functions)

With this, the calls to each cost function would occur at the same time sum() was executing. 这样,对每个成本函数的调用将在sum()执行的同时发生。 Something like that might be useful if you wanted the tuple of functions to vary, in which case you could pass them to the trip_cost() function as an additional argument instead of hardcoding them. 如果您希望改变函数的元组,那么类似的事情可能会很有用,在这种情况下,您可以将它们作为附加参数传递给trip_cost()函数,而不是对其进行硬编码。 However based on your current code and apparent needs, the first way would probably be the best way to go. 但是,根据您当前的代码和明显的需求,第一种方法可能是最好的方法。

Resolve: 解决:

def hotel_cost(nights):
    return 140*nights

def plane_ride_cost(city):
    if city=='Charlotte':
        return 183
    elif city=='Tampa':
        return 220
    elif city=='Pittsburgh':
        return 222
    elif city=='Los Angeles':
        return 475

def rental_car_cost(days):
    day = 40
    cost = days*day
    if days >= 7:
        cost = cost - 50
    elif days >= 3:
        cost = cost - 20
    return cost

def trip_cost(city,days):
    city = plane_ride_cost(city)
    return city+rental_car_cost(days)+hotel_cost(days)

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

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