Home > Software design >  Create a function that calculate the sum of odd and even values of a list and store the result withi
Create a function that calculate the sum of odd and even values of a list and store the result withi

Time:03-04

I am trying to solve this function. I have to create a tuple with the sum of the even and odd numbers of this list. So the output should be (9,12) but my function doesn’t work. I do have to use a function

number_list = [1, 2, 3, 4, 5, 6]

def calculate_odd_even(odd_number, even_number):
    odd_number = 0
    even_number = 0
    t1 = tuple()
    for i in number_list:
        if i % 2 == 0:
            even_number = even_number   number_list[i]
            t1.append(i)
            
        else:
            odd_number = odd_number   number_list[i]
            t1.append(i)
    
    return odd_number
    return even_number

my_tuple = calculate_odd_even(odd_number, even_number)

CodePudding user response:

You need some changes in your code:

  1. No need for function arguments in this case.

    • def calculate_odd_even(odd_number, even_number):
    • def calculate_odd_even():
  2. You iterating over the elements of list, not indices.So there is no need to use number_list[i], just i will do the job.

    • even_number = even_number number_list[i]
    • odd_number = odd_number number_list[i]
    • even_number = even_number i
    • odd_number = odd_number i
  3. Second retrun wont be reached ever.So instead, return a tuple.

  •      return odd_number
         retrun even_number # Wont be reached
    
  • return (odd_number, even_number) # currect way

Final code

number_list = [1, 2, 3, 4, 5, 6]

def calculate_odd_even():
    odd_number = 0
    even_number = 0
    for i in number_list:
        if i % 2 == 0:
            even_number = even_number   i
        else:
            odd_number = odd_number   i
    
    return (odd_number, even_number)

my_tuple = calculate_odd_even()
print(my_tuple) # prints (9, 12)

CodePudding user response:

def Cal_sum(list):
    odd,even=0,0
    for i in list:
        if i%2==0:
           even =i
        else:
           odd =i
    return odd,even

number_list = [1, 2, 3, 4, 5, 6]
tuple_value=tuple(Cal_sum(number_list))
print(tuple_value)

CodePudding user response:

The general idea is ok, that is you go through the list look if the number itself is even (if %2 == 0) and then either add it to the one or the other group.

Though there are a number of things that simply don't work this way in python.

calculate_odd_even(odd_number, even_number):

The value that you'd want to pass as parameter to the function would be the list of numbers. using odd_number and even_number here is especially useless as you directly set them to 0 on the next line. So whatever value is passed to the function will be erased by that.

t1 = tuple()

You seem to want to add the numbers to a list here. But a) tuples are immutable so adding stuff to them simply doesn't work to begin with and b) even if you would replace that with [] or list() you'd still be left with a list but you want a sum. So you'd still need to take sum(t1) in the end.

Next up:

for i in number_list:

goes through the elements of the list so in the first iteration i will be 1 then 2 and so on. So it is NOT an index in the way you used it in this line:

even_number = even_number number_list[i]

So let's go through the first iteration of the loop. In that case i would be 1. Which would trigger the else path as (1%2=1 not equal 0). And there you would attempt to append odd_number number_list[i]. Where the value you'd append (if that were possible) would be 0 2 = 2. Why 2? Because i=1 and therefor you'd take the 2nd value in your number_list as you start counting on 0. So even if all of that would have worked you'd have taken the exact opposite number from your list and for the last number it would crash as there is no index 6 for a list of length 6 (again counting starts at 0).

And again even if that would have worked the odd_number number_list[i] would just add basically the next number in the number_list to a new list of numbers. Not to mention that the odd_number part is redundant as it is 0 and stays that way.

Last but not least a return statement exists the function so anything that is happening after a return statement is ignored. So your second return statement is treated as if it wouldn't exist. Fortunately for you, you don't have to do any hassle to return 2 values in a tuple just use:

return [valueA] [valueB]

to return 2 values in a tuple. So for example return 2, 3 would return (2,3).

Though you could make that a whole lot easier in the first place:

number_list = [1, 2, 3, 4, 5, 6]

def calculate_odd_even(num_list):
    odd_number = 0
    even_number= 0
    for i in num_list:
        if i%2 ==0:
            even_number = even_number   i
        else:
            odd_number = odd_number   i
    return even_number, odd_number


my_tuple = calculate_odd_even(number_list)
  • Related