1

I am trying to add the list in the if statement with the number in the elif statement. How do I add them together to get the resulting output?

def sorting(tup1, tup2):
    output = []
    sumVal = 0
    wholeTup = tup1 + tup2 
    for i in range(0, len(wholeTup)):
      if i % 2 == 0 or i == 0:
        word = wholeTup[i].title()
        output.append(word)
        output.sort()     
      elif i % 2 != 0:
        sumVal = sumVal + wholeTup[i] 
    return output

print(sorting(("Bob",21,"kelly",21), ("morgan",10,"Anna",2)))

The output should look something like:

["Anna", "Bob", "Kelly", "Morgan", 54]

Their names in alphabetical order and all of their ages added together.

I know the if statement will give me this portion of the output:

["Anna", "Bob", "Kelly", "Morgan"]

And the elif statement will give this portion:

54

How can I combine those two together? Is it even possible to combine the output of the if statement and the elif statement?

5
  • 1
    So you want to sort the names in the tuples that are on even indices and sum up the ages? Commented Mar 19, 2017 at 23:23
  • It would actually help if you explained exactly what the criteria is for the output? Having to guess and figuring out by running your code and troubleshooting is not very fun. Commented Mar 19, 2017 at 23:24
  • Just what is wrong with the code you show? Commented Mar 19, 2017 at 23:24
  • How can I combine the output of the if statement and elif statement? @RoryDaulton Commented Mar 19, 2017 at 23:27
  • Yes @WillemVanOnsem Commented Mar 19, 2017 at 23:28

1 Answer 1

1

You can simply add the line:

output.append(sumVal)

to the program just before the return statement to add the total sum of the odd parts of the tuples, like:

def sorting(tup1, tup2):
    output = []
    sumVal = 0
    wholeTup = tup1 + tup2 
    for i in range(0, len(wholeTup)):
        result = " "
        if i % 2 == 0 or i == 0:
            word = wholeTup[i].title()
            output.append(word)
            output.sort()     
        elif i % 2 != 0:
            sumVal = sumVal + wholeTup[i]
    output.append(sumVal) # append the total age to the final result
    return output

Nevertheless your code is rather unelegant, un-Pythonic and inefficient*. A few ideas:

  • you sort the list all the time, whereas sorting it as post-processing step is more efficient;
  • you can simplify the if; and
  • there is no need to check with an elif, a simple else would suffice since the condition in the elif is the exact opposite of the condition in the if statement.

You can transform the entire program into two statements with generators:

def sorting(tup1, tup2):
    wholeTup = tup1 + tup2 
    return sorted(wholeTup[i] for i in range(0,len(wholeTup),2)) + \
        [sum(wholeTup[i] for i in range(1,len(wholeTup),2))]

Here sorted(..) will catch all the elements of the generator wholeTup[i] for i in range(0,len(wholeTup),2) and construct a list with the elements sorted. Here the generator will emit all the elements that are positioned at even places.

Furthermore sum(..) on the other hand will sum up the elements yielded by the generator wholeTup[i] for i in range(1,len(wholeTup),2) and generate the sum of the ages (elements positioned at the odd places). We construct a singleton-list [..] with that result and append it to the result of the sorted(..) call. This is the final result we return.

Sign up to request clarification or add additional context in comments.

4 Comments

What if there was a None instead of an integer for one of the person's name?
@aj2929: both programs will error. What should happen in that case?
Would a try / except work in this case? Like try this.. except TypeError, do this instead @WillemVanOnsem
@aj2929: how can you know that without defining the expected behavior first. In order to solve a problem: you first have to define all corner- and edge cases. Then you can think on how to implement it. It is like asking whether "A hammer will work?" without knowing what task you aim to solve.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.