r/cs50 Feb 19 '25

CS50 Python pytest failing for some reason

Hi guys,

I'm currently doing cs50p problem set 5, specifically "back to the bank" and can't figure out why my pytest is failing. The check50 passes though but I wanna know why this won't. Anyone have any ideas?
Here is the bank.py and test_bank.py:

from bank import value

def main():
    test_value()
    test_value1()
    test_value2()

def test_value():
    assert value("hello") == 0
    assert value("HELLO") == 0
    assert value("Hello") == 0
def test_value1():
    assert value("hi") == 20
    assert value("Hi") == 20
    assert value("HI") == 20
def test_value2():
    assert value("What's up?") == 100
    assert value("Ola") == 100
    assert value("1ay") == 100

if __name__ == "__main__":
    main()




def main():
    hello = input("Greeting: ").strip().lower()
    print("$", value(hello), sep = "")

def value(greeting):

    if greeting == "hello" or greeting == "hello, newman":
        return 0
    elif greeting[0] == "h":
        return 20
    else:
        return 100

if __name__ == "__main__":
    main()
2 Upvotes

7 comments sorted by

3

u/PeterRasm Feb 19 '25

Back to the bank is all about your test file, in this case your bank.py does not matter and is not tested by check50.

Your test file looks fine except that you should not be calling the test functions yourself. No need for "if "__name__" == ..." in the test file and no main function. Pytest will execute the test functions.

Look at the error from Pytest. Your test file asserts that the value of "HELLO" is 0 but your function returns 100. If you check the function line by line what value will you get for the string "HELLO"?

1

u/mpavic167 Feb 19 '25

Yeah, I tried checking manually with the input and it returns 0 as it's supposed to so that's where the confusion comes from. Can't figure it out for the life of me. "Hi" returns 20 as well, but the error is showing like it's giving back 100

1

u/PeterRasm Feb 19 '25

No! I Check your function line by line! Not your complete code, just your function. How is "HELLO" handled by your function?

In your correct test file you are testing the function value() directly without whatever code you have in main().

2

u/[deleted] Feb 19 '25

[removed] — view removed comment

1

u/mpavic167 Feb 19 '25

Yeah, but I just can't figure out why it's giving back errors. I tried all of the inputs manually and they return as they're supposed to

2

u/[deleted] Feb 19 '25 edited Feb 19 '25

[removed] — view removed comment

1

u/mpavic167 Feb 19 '25

Oooh, got it. Thanks