if I want to check if the input is a list of integers with exactly two occurrences of nineteen and at least three occurrences of five with one Output, either True or False, I wrote the following code:
nums = [19,19,15,5,3,5,5,2]
def listChecker():
if nums.count(19) == 2 and nums.count(5) >= 3:
return True
else:
return False
But when I tried to test the code with
listChecker(nums)
I get an error message saying: TypeError: listChecker() takes 0 positional arguments but 1 was given
What's wrong? Thank you so much, I am still very new, the learning curve is hard!! Best
CodePudding user response:
theoretically we don't need to pass the list name to the function, because nums
is created in a global scope, so that the function can read the list. but it could be seen as a bad coding practice.
we are going to use this code. the evaluation and returning is done a bit more compact.
nums = [19,19,15,5,3,5,5,2]
def listChecker(nums_):
return nums_.count(19) == 2 and nums_.count(5) >= 3
print(listChecker(nums))
CodePudding user response:
def list_occurrences(l):
return True if l.count(19) == 2 and l.count(5) >= 3 else False
print(list_occurrences([119, 19, 5, 5, 5, 5, 5]))
write it this way or in your code change listChecker(nums) to listChecker()