Python Forum
Multiply and Addition in the same loop statement with logic.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multiply and Addition in the same loop statement with logic.
#1
Hi Python Community,

Greetings to you all !

After the if statement is passed through Reven and Rodd have to take two alternative indexes: one has to do multiply, and another has to do add.

like example,

Output:
array has {0,1,2,3,4,5,6,7,8,9} Reven = ((((A[0]*A[2])+A[4])*A[6])+A[8]) like 0 +2 2 *4 8 +6 14 *8 array has {0,1,2,3,4,5,6,7,8,9} Reven = ((((A[1]*A[3])+A[5])*A[7])+A[9]) like 1 + 3 4 * 5 20 +7 27 * 8
wriiten code

def AddMult(A):
    Reven = 0
    Rodd = 0
    for i in range(len(A)):
        if i % 2 == 0:
            Reven = Reven + A[i]
        else:
            Rodd = Rodd + A[i]

    E = (Reven%2)
    O = (Rodd%2)
    if(O > E):
        return "ODD"
    elif(E > O):
        return "EVEN"
    elif(O == E):
        return "NEUTRAL"
Could you please help me solve this?
buran write Jan-31-2023, 10:02 AM:
Please, use proper tags when post code, traceback, output, etc.
See BBcode help for more info.
Reply


Messages In This Thread
Multiply and Addition in the same loop statement with logic. - by joelraj - Jan-31-2023, 09:52 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Optimise multiply for loop in Python KenBCN 4 518 Feb-06-2024, 06:48 PM
Last Post: Gribouillis
Photo Python code: While loop with if statement HAMOUDA 1 620 Sep-18-2023, 11:18 AM
Last Post: deanhystad
  forloop to compute sum by alternating from addition to subtraction JulianZ 3 1,869 Apr-02-2022, 09:36 AM
Last Post: DeaD_EyE
  Compare each element of an array in a logic statement without using a for loop leocsmith 3 5,940 Apr-01-2021, 07:57 PM
Last Post: deanhystad
  how to create pythonic codes including for loop and if statement? aupres 1 1,950 Jan-02-2021, 06:10 AM
Last Post: Gribouillis
  if statement in for loop researcher123 6 2,659 Oct-01-2020, 05:07 PM
Last Post: deanhystad
  Help: list comprehension for loop with double if statement mart79 3 2,475 May-04-2020, 06:34 AM
Last Post: buran
  Need help with For Loop logic for reading docx Table column data vintysaw 2 3,947 Jan-10-2020, 06:36 AM
Last Post: vintysaw
  (Python help) Change in logic not breaking 'while' loop? btcg2807 1 1,916 Sep-18-2019, 09:43 AM
Last Post: Larz60+
  addition for elements in lists of list ridgerunnersjw 3 3,134 Sep-15-2019, 07:11 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

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