Python Object-Oriented Python Inheritance Instances

Panashe MUGUTI
Panashe MUGUTI
3,401 Points

hello guys i don't know what this question is asking for help pliz!

Object-Oriented Python

Challenge Task 1 of 1 Alright, here's a fun task!

Create a function named combiner that takes a single argument, which will be a list made up of strings and numbers.

Return a single string that is a combination of all of the strings in the list and then the sum of all of the numbers. For example, with the input ["apple", 5.2, "dog", 8], combiner would return "appledog13.2". Be sure to use isinstance to solve this as I might try to trick you.

Important: In each task of this code challenge, the code you write should be added to the code from the previous task.
instances.py def combiner

instances.py
def combiner

3 Answers

William Li
William Li
6,045 Points

We would have to check whether each element in the inputted list is a string or a number(int or float).

Then if the item is a string we would append it to the string we want to return. If it is a number we would sum it to the number we would return.

Then we return both the strings and the summed numbers

def combiner(list):
    string = ''
    summ = 0
    for item in list:
        if isinstance(item,str):
            string+=item
        elif isinstance(item,(int,float)):
            summ+= item
    return string+str(summ)
Mohit Yadav
Mohit Yadav
5,380 Points

Your functions should take a single argument which is a list of strings and numbers and return all the items combined in a single string. You can us isinstance method for this and create a if else statement. Also don't forget to change num into int before adding that to your final string

Panashe MUGUTI
Panashe MUGUTI
3,401 Points

thank you very much it worked.