Python Homework - creating a new list

前端 未结 6 826
闹比i
闹比i 2021-01-22 19:36

The assignment:

Write a function called splitList(myList, option) that takes, as input, a list and an option, which is either 0 or 1. If the

6条回答
  •  太阳男子
    2021-01-22 20:07

    As I mentioned in my comment, you should standardize your indentation: four spaces is Python standard. You can usually set your editor to insert four spaces instead of tabs (don't want to mix tabs with spaces, either).

    As to your actual question: try writing three total functions: one that returns all the negative values, one that returns even values, and one that calls the appropriate function based on option.

    def splitlist(myList, option):
        if option == 1:
            return get_even_elements(myList)
        elif option == 0:
            return get_negative_elements(myList)
    
    def get_even_elements(myList):
        pass # Implementation this method here.
    
    def get_negative_elements(myList):
        pass # Implementation this method here.
    
    # Test it out!
    alist = [-1, 2, -8, 5]
    print splitlist(alist, 0)
    print splitlist(alist, 1)
    

提交回复
热议问题