Reputation: 5
no idea what an "assertion error" even is, but i'm trying to run this through my class test module and getting the following error:
File "/var/gt_test_envs/iVpgS6thyOoxSD9V/wrk/homeworkTests.py", line 201, in test_remove_char self.assertEqual(expected, returned) AssertionError: ['pZlp', 'Bn', 'GaFu', 'sMopFKjxl', 'IZhhtE'] != None
code is below:
def remove_char(str_list: object, char: object)-> object:
str_list = ()
char = []
for value in char:
str_list = str_list.replace(value, '')
return object
Upvotes: 0
Views: 668
Reputation: 71517
The AssertionError
is being raised because your function returns the wrong result, and the test assert
s that it returns the right result (this is how unit tests usually work). According to the error message, it's expected to return a list of strings but instead it returns None
(that is, it's not just returning the wrong result, but entirely the wrong type of object).
There are a lot of bugs piled on top of each other in order to produce this result -- I don't think a single line of this function is doing what you want it to do. I'll do my best to explain all the bugs individually.
The indentation of your code snippet is completely mangled; the entire body of the function must be indented (unindenting indicates that the function is over). I'd expect a completely empty function body to produce an IndentationError, so the code that you're actually running the test on must be different from what you pasted into your question above. If the entire body of the function were indented two more levels it would run and would return None
(as seen in the test failure message) so I assume that's what you're actually working with.
The type annotations are technically valid but not useful; typing something as an object
is pretty much like not having type annotations. The main purpose of type annotations is to help you (and others) understand how the code works; it goes without saying that everything is an object
, so annotating everything that way doesn't add any understanding. If you can say more specifically that str_list
is a list
, and char
is a str
, and the function returns another list
, it's easier to understand what the function is doing and how it works. If you can be a little more specific and say that it's a List[str]
(a list of strings) that's even more helpful!
Starting your function by reassigning str_list
and char
to empty values (overwriting the parameters) ensures that it's impossible to return a valid result. Note also that you're assigning a tuple
to str_list
, and a list
to char
.
Even if the loop that follows were able to build the correct result as str_list
, you're returning object
instead of str_list
, so the caller is receiving the "object" type rather than the actual str_list
object (which should be a list but is actually a tuple).
On top of that, since the return is happening inside the loop body (which is never executed because you're iterating over an empty tuple, which happens because you overwrote str_list
right at the start), it never actually happens, which is why the function returns None
.
Here's a working (and correctly typed) version of the function:
from typing import List
def remove_char(str_list: List[str], char: str) -> List[str]:
new_list: List[str] = []
for value in str_list:
new_value = value.replace(char, '')
new_list.append(new_value)
return new_list
Note that we're building an entirely new list (rather than modifying the original str_list
), and doing the replace
on the individual value
s from the list (which we get by iterating in a for
loop over str_list
), not the list itself.
Here is a simpler version of the same function; this does almost exactly the same thing as the above code, but as a list comprehension statement:
from typing import List
def remove_char(str_list: List[str], char: str) -> List[str]:
return [value.replace(char, '') for value in str_list]
As I said in the comments, my advice for the future is to read and write your code carefully, one line at a time -- don't write a line of code if you don't know exactly what it does and have a reason for wanting it to do that exact thing. It is very difficult to find bugs in a program if you don't know what each line of code is doing. If you can't figure out how to do a particular thing, ask your teacher for help with that particular thing rather than writing a broken program and then trying to get someone else to rewrite it for you. It'll work some of the time (like it did this time), but it's not likely to get you all the way through your programming class.
Good luck!
Upvotes: 2