Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Syntax error
#1
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
Reply
#2
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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
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
Reply
#4
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Syntax error for "root = Tk()" dlwaddel 15 1,165 Jan-29-2024, 12:07 AM
Last Post: dlwaddel
Photo SYNTAX ERROR Yannko 3 375 Jan-19-2024, 01:20 PM
Last Post: rob101
  Syntax error while executing the Python code in Linux DivAsh 8 1,563 Jul-19-2023, 06:27 PM
Last Post: Lahearle
  Code is returning the incorrect values. syntax error 007sonic 6 1,206 Jun-19-2023, 03:35 AM
Last Post: 007sonic
  syntax error question - string mgallotti 5 1,298 Feb-03-2023, 05:10 PM
Last Post: mgallotti
  Syntax error? I don't see it KenHorse 4 1,243 Jan-15-2023, 07:49 PM
Last Post: Gribouillis
  Syntax error tibbj001 2 883 Dec-05-2022, 06:38 PM
Last Post: deanhystad
  Python-for-Android:p4a: syntax error in main.py while compiling apk jttolleson 2 1,832 Sep-17-2022, 04:09 AM
Last Post: jttolleson
  Mysql Syntax error in pymysql ilknurg 4 2,348 May-18-2022, 06:50 AM
Last Post: ibreeden
  Solving equation equal to zero: How to resolve the syntax error? alexfrol86 3 1,952 Feb-21-2022, 08:58 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020