且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Python家庭作业-创建新列表

更新时间:2023-11-28 17:21:34

正如我在我的评论中提到的,您应该对缩进进行标准化:Python标准是四个空格.通常,您可以将编辑器设置为插入四个空格而不是制表符(也不希望将制表符与空格混用).

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).

关于您的实际问题:尝试编写总共三个函数:一个返回所有负值,一个返回偶数,另一个基于option调用适当的函数.

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)