0
def manipulate_data(n): positives,negatives=[],[] if not isinstance(n,list): return else: for i in n: if i>=0: positives.append(i) if i<0 negatives.append(i) new=[len(positives,sum(negatives)] return new. please the unittest is still telling me that my code failed the "only lists" allowed test.what do I do.
7 Antworten
+ 1
def manipulate_data(n: list) -> list:
countPos, sumNeg = 0, 0
for i in n:
if i>=0:
countPos += 1
else:
sumNeg += i
return [countPos, sumNeg]
+ 1
This synthax precises the type of the argument and return value. This is called type hints, and was introduced in Python 3.5: https://docs.python.org/3/library/typing.html
Test my code here, it's working:
http://code.sololearn.com/cQE1I52IgJIk
+ 1
Maybe add this at the beginning of your function block too:
if not isinstance(n,list):
raise ValueError
+ 1
hey @ Dan-Awoh Emanuel, y is my code not working please.
here it isdef manipulate_data(n):
positives,negatives=[],[]
if not isinstance(n,list):
return
else:
for i in n:
if i>=0: positives.append(i)
if i<0 negatives.append(i) new=[len(positives,sum(negatives)] return new
0
please assist me with this problem
this is is the original question
Write a function called manipulated_data which will act as follows:
When given a list of integers, return a list, where the first element is the count of positives numbers and the second element is the sum of negative numbers.
0
Zen please what does that syntax do. I've never seen it before
manipulate_data(n:list)->list:
and the interpreter is telling me that there is a syntax error
0
ok. I've added it and the code worked fine. unittests are key to determining the level of proficiency. I thought I was ok until I started designing codes to meet specifications. NB: the fact that it works doesn't mean it is a good piece of code!!