如何使变量=函数调用的值?

时间:2014-02-09 04:57:34

标签: python

我们必须使用先前制作的函数(mega_calculator)来计算10栋建筑物的平均财产损失金额。但是,我们需要找出哪个建筑物将被毁坏最多,但我们不断收到有关将函数与int进行比较的错误消息。由于某种原因,y变量(用于存储mega_calculator值)被标记为函数,并且if语句未被触发。

我们正在尝试使用 for循环,但它不会改变任何内容。我们还尝试在mega_calculator中声明返回值必须是整数类型,但是没有做任何事情。我们尝试将平均值保存为变量,并将其断言为整数类型,但没有做任何事情。

我应该怎么做?

任何帮助都会受到极大的喜爱和赞赏。我们必须有一个奇怪的函数设置,所以不幸的是我不能只做一个简单的while循环。

    def mega_calculator(fn, repeat=1000):
        def helper(*args):
            total = 0
            for _ in range(repeat):
                total += fn(*args)
            return total / repeat
        return helper
    def worst_hurricane(odds):   """odds is a predefined function that tells us a random amount of property damage"""
        index_variable = 1
        big_boom = 0
        place = 0
        while index_variable <= 10:
             y = mega_calculator(odds,50) """checking odds of damage for skyscrapers only, and finding the average after 50 times is what the function cal to mega_calculator does"""
             print("building", a, "will have", y, "dollars of damage")
             if y > big_boom:
                 big_boom = y
                 place = index_variable
             elif y == big_boom:
                 place = max(place, index_variable)
                 index_variable +=
         return place

`

2 个答案:

答案 0 :(得分:2)

mega_calculator正在返回一个名为helper的函数,您可以调用它。尝试这样的代码:

calculator = mega_calculator(odds)
y = calculator(50)

您可能还想在左侧取消index_variable +=个4个位置,并将其更改为index_variable += 1

答案 1 :(得分:1)

以下是您要做的事情:

我正在使用一些虚拟函数,只是为了让你理解:

 >>> def mega_calculator(some_function):
 ...     def helper(*args):
 ...         return some_function(*args)
 ...     return helper
 ...
 >>> def odds(*args):
 ...     print args
 ...
 >>> x = mega_calculator(odds)
 >>> x
 <function helper at 0x10c8f18c0>
 >>>
 >>> x = mega_calculator(odds)(['Here', 'are some' , 'argument'])
 (['Here', 'are some', 'argument'],)
 >>>