Python Homework - creating a new list

前端 未结 6 828
闹比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

    I tried your code as-is and I did not get any errors when I passed in a list of positive integers, so I don't know why your program is 'crashing', so I suspect something else is interfering with your debugging. (Although, as others have said, you really should use the same number of spaces at every indent level.)

    Here's what I entered:

    def splitList(myList):
        newlist = []
        for i in range(len(myList)):
            if (myList[i]%2 == 0):
               newlist.append(myList [i])  # Only 3-space indent here
        return newlist
    
    print splitList([1, 2, 3, 4, 5])
    

    When I run it:

    [2, 4]
    

    Can you show how you're invoking the method and the exact error message?

提交回复
热议问题