Python Homework - creating a new list

前端 未结 6 871
闹比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 19:52

    My previous answer was incorrect, and I really should have tested before I opened my mouth... Doh!!! Live and learn...

    edit

    your traceback is giving you the answer. It is reading the list as an args list. Not sure if there is a super pythonic way to get around it, but you can use named arguments:

    splitList(myList=yourList)
    

    OR

    splitList(myList=(1,2,3))
    

    OR

    splitList(myList=[1,2,3])
    

    But your problem is really probably

    splitList(1,2,3,4)  # This is not a list as an argument, it is a list literal as your 'args' list
    

    Which can be solved with:

    splitList([1,2,3,4])
    

    OR

    splitList((1,2,3,4))
    

    Is this the traceback you get?

    Traceback (most recent call last):
      File "pyTest.py", line 10, in 
        print splitList(aList,1)
    TypeError: splitList() takes exactly 1 argument (2 given)
    

    The error is most likely telling you the issue, this would be that the method only takes one argument, but I tried passing in two arguments like your requirements state.

提交回复
热议问题