【问题标题】:Trouble passing inputs of User-Defined function to other functions and summing returned values无法将用户定义函数的输入传递给其他函数并求和返回值
【发布时间】:2013-05-26 00:13:34
【问题描述】:

我是一名学习 Python 的 N00b。作为创建简单计算器的小项目的一部分,我需要一个函数来获取两个输入并确定旅行的总成本。

在这段代码的底部,我有一个名为“trip_cost()”的函数,它接受两个输入,城市和天数。 City 是您要访问的城市的字符串,days 是您停留天数的整数值。

trip_cost() 将输入传递给rental_car_cost、hotel_cost 和plane_ride_cost 函数,并返回它们各自输出的总和。

我知道一个函数可以通过名称作为变量从另一个函数中调用,但我对如何将 trip_cost() 的输入处理到其他函数并返回三个值在 trip_cost( )。

任何建议将不胜感激。

谢谢,

-- 马特

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)

【问题讨论】:

    标签: python function sum


    【解决方案1】:

    我已经为你创建了一个“更好”的版本,因为你正在学习 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)
    

    演示:

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

    这将返回:

    # 740
    

    【讨论】:

    • 感谢彼得的建议。在函数中使用字典确实看起来更好。我这样做的方式似乎更复杂。
    • @matt 没关系,当我开始编码时,我做过更肮脏的事情。重要的是,永远不要停止学习和练习!如果觉得我的回答有用,请点赞,采纳!谢谢!
    【解决方案2】:

    您在这里并不真正需要sum(),因为以下内容会起作用并且需要很少的开销:

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

    但是,您可以使用sum()与以下类似的东西,它创建一个调用每个成本函数的结果的元组,然后将该对象传递给sum()进行总计。

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

    另一种选择是使用generator expression,但它要求每个函数都接受相同的输入参数,即使它们没有同时使用它们。 如果它们被改写成这样,它可以让你按照这些思路做一些事情:

    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)
    

    这样,对每个成本函数的调用将在sum()正在执行的同时发生。如果您希望函数的元组发生变化,那么类似的东西可能会很有用,在这种情况下,您可以将它们作为附加参数传递给trip_cost()function,而不是硬编码它们。但是,根据您当前的代码和明显的需求,第一种方法可能是最好的方法。

    【讨论】:

    • 感谢您这样做(即 sum() 与函数参考)。我很欣赏这些建议......似乎我遇到了一个简单的语法错误。
    • @martineau 除了那是一个元组,而不是一个 genexp。
    【解决方案3】:

    解决:

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-13
      • 1970-01-01
      • 2019-12-18
      • 2014-06-01
      相关资源
      最近更新 更多