Python Forum
Python for loops giving error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python for loops giving error
#5
(Jan-08-2019, 05:37 AM)perfringo Wrote:
(Jan-07-2019, 10:56 PM)Petrus Wrote: No, I just want to multiply every single value inside the list with each other.

Two possible ways to achieve that below. It is recommended to keep what you do and how you do separate. Therefore functions are used:

# without any import

def sequence_product(sequence):
    """Return sequence elements product; if empty return zero."""
    
    if not sequence:
        return 0
    else:
        total = 1
        for el in sequence:
            total *= el
        return total

# with reduce and mul

from functools import reduce
from operator import mul

def sequence_product(sequence):
    """Return sequence elements product; if empty return zero."""

    if not sequence:
        return 0
    else:
        return reduce(mul, sequence)

But the question remains I don't know how to glue this code with other code containing the combinations like [4,2,2,6]and so on. Because at the end of the day I am trying to write a function that can read the combinations and multiply all the numbers inside the list.
Only 1 function that can read sequences, append the values from a dictionary (which already works) and the combinations should multiply and give answers like 96 and so on.
Reply


Messages In This Thread
Python for loops giving error - by Petrus - Jan-07-2019, 10:01 PM
RE: Python for loops giving error - by xhughesey - Jan-07-2019, 10:18 PM
RE: Python for loops giving error - by Petrus - Jan-07-2019, 10:56 PM
RE: Python for loops giving error - by perfringo - Jan-08-2019, 05:37 AM
RE: Python for loops giving error - by Petrus - Jan-08-2019, 10:23 AM
RE: Python for loops giving error - by Petrus - Jan-08-2019, 11:38 AM
RE: Python for loops giving error - by perfringo - Jan-08-2019, 11:58 AM
RE: Python for loops giving error - by Petrus - Jan-08-2019, 12:29 PM
RE: Python for loops giving error - by perfringo - Jan-08-2019, 12:48 PM
RE: Python for loops giving error - by Petrus - Jan-08-2019, 01:24 PM
RE: Python for loops giving error - by perfringo - Jan-08-2019, 01:48 PM
RE: Python for loops giving error - by Petrus - Jan-08-2019, 02:27 PM
RE: Python for loops giving error - by Petrus - Jan-09-2019, 08:02 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  New to python! Loops Seeley307 3 62,226 May-15-2020, 02:27 PM
Last Post: ibreeden
  Error in loops, new to Python jhall710 20 12,398 Apr-25-2017, 05:18 AM
Last Post: smbx33

Forum Jump:

User Panel Messages

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