0

I am trying to save the results of 10 function calls into a list. however, when I want to access this numerical data, I keep getting type errors, because the list is really a list of function calls. How do I make it do stuff so I can have numbers to do compare?

I tried setting temp variable to result of each spot in the list, but it still shows as a function.

Output should show the average for different # of dice, and return the best amount of dice to roll

def calculator(fn, num_samples=1000): # this is the function I'm trying to call
    def print_and_return(*args):
            total3 = 0
            for _ in range(num_samples):
                total3 += (fn(*args))
            return float(total3)/num_samples
        return print_and_return

def find_average(dice=six_sided):
    avglist = []
    k, spot = 0, 0
    bob = roll_dice(k+1, dice)
    passed_function = calculator(bob, 1000)

    print(passed_function)
    while k <= 10:
        avglist.append((passed_function)) # <==trying to save the results when I check with 1-10 dice rolls
        if k == 0:
            spot = 1
        else:
            temp = 0
            max = 0
            i = 0
            while i <= len(avglist):
                temp = avglist[i]
                if max > temp:
                    max = temp
                i +=1

            if (avglist[k] > temp):
                spot = k
        print(k, "dice scores", avglist[k], "on average")
        k +=1 
    return spot
4
  • 4
    Can you please show the code with sample data? Commented Feb 12, 2014 at 5:26
  • What does "it showed up as a function" mean? Commented Feb 12, 2014 at 6:24
  • What is return print_and_return supposed to do? Commented Feb 12, 2014 at 6:26
  • Does roll_dice return a function as well? The answers below are correct in saying that you need to call passed_function (inside the while loop, most likely with k as an argument), but even if you do, your code will not work if bob is not a function: You are passing bob as the fn argument to calculator, and inside print_and_return, fn is applied to an arg list (i.e., it is used as a function). Therefore, if bob is e.g. an int or a float, you will get a TypeError when trying to add the result of fn(*args) to total3 (line 5). Commented Feb 12, 2014 at 8:19

2 Answers 2

2

You are not calling passed_function. Calling a function with no arguments still reqires the ().

Sign up to request clarification or add additional context in comments.

Comments

2

Your calculator function returns a function, not a plain value. You therefore need to call passed_function in order to get your result.

Based on the arguments to your call to roll_dice, it looks like you want the k argument to vary in the loop. As written now, it will not. I suspect you are tripping yourself up a bit. The code could be written a whole lot simpler without so many function references being passed around.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.