Why does this code fail the test case where “bill” is not an integer(supposed to return: “This is not a bill”)
Anonymous in /c/coding_help
567
report
EMPLARYI got most of the test cases correct, but the ones where “bill” is not an integer remain(suppose to return “This is not a bill”). Why?<br><br>Here is my code:<br>```python<br>def is_an_expense(bill):<br> return not isinstance(bill, int)<br>```<br>Here’s the function code:<br><br>def is_an_expense(bill):<br> # Your code here<br><br># Test Cases<br>bill = "This is not a bill"<br>print(is_an_expense(bill)) # return: This is not a bill<br>bill = 100<br>print(is_an_expense(bill)) # return: False<br>bill = "100"<br>print(is_an_expense(bill)) # return: This is not a bill<br>bill = "one hundred"<br>print(is_an_expense(bill)) # return: This is not a bill<br>bill = "elephant"<br>print(is_an_expense(bill)) # return: This is not a bill<br>bill = 150<br>print(is_an_expense(bill)) # return: False<br>bill = 250<br>print(is_an_expense(bill)) # return: False<br>bill = "ACM - 50"<br>print(is_an_expense(bill)) # return: This is not a bill<br>bill = "UW - 200"<br>print(is_an_expense(bill)) # return: This is not a bill<br>bill = "UW - 50"<br>print(is_an_expense(bill)) # return: This is not a bill<br>bill = "ACM - 100"<br>print(is_an_expense(bill)) # return: This is not a bill<br>bill = "ACM-200"<br>print(is_an_expense(bill)) # return: This is not a bill<br>bill = "Please pay UW 150 dollars for tuition"<br>print(is_an_expense(bill)) # return: This is not a bill<br>```<br>I’m just not sure why it fails the test case where “bill” isn’t an integer. Is it because the code only returns “This is not a bill” when it first checks if “bill” is not an integer? If that’s the case, then I just have to move the “return” statement below the print statements.<br><br>[Edit: I was on the right track, just got the logic backwards. The more complex code is needed]<br><br>^Maybe I should just change the entire function to take the absolute value of the bill when it is returned.<br><br>[Edit 2: It seems that I’m having to put my hands in the sand, and cover my eyes, because I know I’m doing something wrong. Why can’t I just return “This is not a bill” when “bill” is not an integer? The function is supposed to return “This is not a bill” when it’s not an integer.<br><br>[Edit3: Wow, I was way wrong… I guess I need to put my hands on the sand and put my face in it and cover everything, because I was wrong on so many levels… I feel like I need to “rip up all the bad code I wrote in the past” and “throw it in the garbage where it belongs”. But I would still like to know why the original code was some rubbish, so I can improve as a coder.<br><br>[Edit4: I know what I would put in the return statement “This is not a bill”. I would have to think about where to put it.<br><br>[Edit5: This could be the “bill” code.<br><br>```python<br>def is_an_expense(bill):<br> if not isinstance(bill, int):<br> print("This is not a bill")<br> return "This is not a bill"<br> else:<br> return -abs(bill)<br>```<br>As for the “bill” function code, I just have to print the result:<br><br>```python<br>def is_an_expense(bill):<br> if not isinstance(bill, int):<br> print("This is not a bill")<br> return "This is not a bill"<br> else:<br> return -abs(bill)<br><br># Test Cases<br>bill = "This is not a bill"<br>print(is_an_expense(bill)) # return: This is not a bill<br>bill = 100<br>print(is_an_expense(bill)) # return: False<br>bill = "100"<br>print(is_an_expense(bill)) # return: This is not a bill<br>bill = "one hundred"<br>print(is_an_expense(bill)) # return: This is not a bill<br>bill = "elephant"<br>print(is_an_expense(bill)) # return: This is not a bill<br>bill = 150<br>print(is_an_expense(bill)) # return: False<br>bill = 250<br>print(is_an_expense(bill)) # return: False<br>bill = "ACM - 50"<br>print(is_an_expense(bill)) # return: This is not a bill<br>bill = "UW - 200"<br>print(is_an_expense(bill)) # return: This is not a bill<br>bill = "UW - 50"<br>print(is_an_expense(bill)) # return: This is not a bill<br>bill = "ACM - 100"<br>print(is_an_expense(bill)) # return: This is not a bill<br>bill = "ACM-200"<br>print(is_an_expense(bill)) # return: This is not a bill<br>bill = "Please pay UW 150 dollars for tuition"<br>print(is_an_expense(bill)) # return: This is not a bill<br>```<br>​
Comments (11) 19186 👁️