Python Forum
Syntax error - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Syntax error (/thread-28784.html)



Syntax error - Blaedel - Aug-03-2020

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


RE: Syntax error - DeaD_EyE - Aug-03-2020

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.


RE: Syntax error - Yoriz - Aug-03-2020

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



RE: Syntax error - Blaedel - Aug-03-2020

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