I’m just playing around with some code but it seemed to have the specific number of variables hardcoded. I’m wondering how if its possible to use an unknown number of variables.
Here’s a sample of the offending code to clarify(its not functional on its own, just took it out of a larger code base to show what I mean):
def test(a,b,c,current_total):
a_range = range(0, target_sum + 1, a)
b_range = range(0, target_sum + 1, b)
c_range = range(0, target_sum + 1, c)
for i, j, k in itertools.product(a_range, b_range, c_range):
if i + j + k == current_total:
counter = counter +1
As you can see it has 3 variables that are referred to explicated in the code, but what if its a larger list of variables. For example, instead of [1,2,3], it was [1,2,3,4,5,6..]
I know I can find out how many variables are in the new list(len(list)), but what do I do from there if I know there’s N variables in the list? Is there a way to use an unknown number of variables in the for loop(and rest of the program?)
While you could use an arbitrary number of arguments via *args, why not simply use a list? It’s more natural, I think. Something like:.
[BTW, note that this is just an example. If you really want the result of the specific function you gave, there are much faster ways to compute it.]