user1476390
user1476390

Reputation: 301

how to make variable = the value of a function call?

We have to use a previously made function (the mega_calculator) to calculate the average amount of property damage for 10 buildings. however, we need to find out which building is going to be the most destroyed, but we keep getting error messages about comparing functions to ints. for some reason, the y variable(used to store mega_calculator values) is being labeled as a function, and the if statements aren't being triggered.

We are trying to use a for loop but it doesn't change anything. We also tried asserting inside mega_calculator that the return value must be an integer type, but that didn't do anything. we tried saving the average value as a variable and asserting that as an integer type but that didn't do anything.

What should I do for that?

Any help is loved and appreciated greatly. We must have the weird function setup, so unfortunately I can't just make a nice simple while loop.

    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

`

Upvotes: 0

Views: 70

Answers (2)

James Sapam
James Sapam

Reputation: 16940

Here is what you are trying to do:

I am using some dummy function called, just to make you understand:

 >>> 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'],)
 >>>

Upvotes: 1

vinod
vinod

Reputation: 2368

mega_calculator is returning a function named helper, that you can call. Try code like this:

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

You also probably want to unindent index_variable += 4 positions to the left, and change it to index_variable += 1.

Upvotes: 2

Related Questions