Python Forum

Full Version: Syntax error
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi guys,

struggling to learn. Why is there a syntax error in the following:

Quote:combinations_amounts = []

for amount_100 in range (0, 100+1, 100):
[ind] for amount_50 in range (0, 100+1, 50):
[ind][ind] for amount_25 in range (0, 100+1, 25):
[ind][ind][ind] for amount_10 in range (0, 100+1, 10):
[ind][ind][ind][ind]for amount_5 in range (0, 100+1, 5):
[ind][ind][ind][ind][ind]for amount_1 in range (0, 100+1, 1):
[ind][ind][ind][ind][ind]total_so_far = amount_100 + amount_50 + amount_25 + amount_10 + amount_5 + amount_1

[ind][ind][ind][ind][ind]if total_so_far <= 100:
[ind][ind][ind][ind][ind][ind] combinations_amounts.append([amount_100, amount_50, amount_25, amount_10, amount_5 amount_1, 100 - total_so_far])
TEACH ME!!! By the way, I can't figure out how to show the indentations I have made in this post so I made these clumsy [ind], I hope you understand. I believe the indentation is correct, but who knows. maybe you?

Thanks and very much appreciated
Maybe some indentation is wrong. Hard to say until you've posted your code with code tags.
The Python-Symbol in the editor area opens a code block for python code.
It could be written with 5 lines of code if you use product from itertools.

Example:
from itertools import product

STEPS = (100, 50, 25, 10, 5, 1)

ranges = [range(0, 101, steps) for steps in STEPS]
# [
#     range(0, 101, 100), range(0, 101, 50),
#     range(0, 101, 25), range(0, 101, 10),
#     range(0, 101, 5), range(0, 101)
# ]

product_iter = product(*ranges)
# (0, 0, 0, 0, 0, 0)
# (0, 0, 0, 0, 0, 1)
# ...
# (100, 100, 100, 100)

# results = [val_sum for values in product_iter if (val_sum := sum(values)) > 100]
# print(len(results))
# print(sum(results))

results = []
for values in product_iter:
    total = sum(values)
    if total > 100:
        results.append(total)
If you have nested for-loops, try to use product. It will save indentations.
The list comprehension is a bit hard to understand.
Based on your [ind]'s see commented lines
combinations_amounts = []

for amount_100 in range(0, 100+1, 100):
    for amount_50 in range(0, 100+1, 50):
        for amount_25 in range(0, 100+1, 25):
            for amount_10 in range(0, 100+1, 10):
                for amount_5 in range(0, 100+1, 5):
                    for amount_1 in range(0, 100+1, 1):
                    total_so_far = amount_100 + amount_50 + amount_25 + amount_10 + amount_5 + amount_1 # from this line onwards needs indenting another level

                    if total_so_far <= 100:
                        combinations_amounts.append([amount_100, amount_50, amount_25, amount_10, amount_5 amount_1, 100 - total_so_far]) # comma missing after amount_5
Ok - thats pretty cool.

I'll try with that. I think I may have located my error to a small comma problem, but then, after correcting it, new problems arose. I will go with your solution.

Cheers
/Martin