最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

pytest - cs50's python "bank testing" question is flaging the 4th condition, what is it wanting me to

programmeradmin4浏览0评论

file to be tested: '''

def main():
    use_input = input("Greeting: ").strip()
    x = value(use_input.lower())
    print(f"${x}")


def value(greeting):
    # if greeting starts with hello , $0
        if greeting[0:5] == "hello":
            return 0

    # if greeting starts with h , $20
        elif greeting[0] == "h":
            return 20

    # else, $100
        else:
            return 100


if __name__ == "__main__":
    main()

''' file which is doing the testing: '''

from bank import value


def test_greeting_charge():
    assert value("hello") == 0
    assert value("hi") == 20
    assert value(" ") == 100
    assert value("test") == 100
    assert value("hi where is 100th st?") == 20
    assert value("oloo") == 100
    try:
        assert value("Hi") == 20
    except AssertionError:
        print("Hi is capitalized so assertionerror")


if __name__ == "__main__":
    main()

''' cs50s check output (4th condition is the concern):

:) test_bank.py exist
:) correct bank.py passes all test_bank checks
:) test_bank catches bank.py with incorrect values
:( test_bank catches bank.py without case-insensitivity
    expected exit code 1, not 0
:) test_bank catches bank.py not allowing for entire phrase

I really dont understand what I need to do to fix my code, pytest is working fine but the actual check for the problem fails the fourth condition

file to be tested: '''

def main():
    use_input = input("Greeting: ").strip()
    x = value(use_input.lower())
    print(f"${x}")


def value(greeting):
    # if greeting starts with hello , $0
        if greeting[0:5] == "hello":
            return 0

    # if greeting starts with h , $20
        elif greeting[0] == "h":
            return 20

    # else, $100
        else:
            return 100


if __name__ == "__main__":
    main()

''' file which is doing the testing: '''

from bank import value


def test_greeting_charge():
    assert value("hello") == 0
    assert value("hi") == 20
    assert value(" ") == 100
    assert value("test") == 100
    assert value("hi where is 100th st?") == 20
    assert value("oloo") == 100
    try:
        assert value("Hi") == 20
    except AssertionError:
        print("Hi is capitalized so assertionerror")


if __name__ == "__main__":
    main()

''' cs50s check output (4th condition is the concern):

:) test_bank.py exist
:) correct bank.py passes all test_bank checks
:) test_bank catches bank.py with incorrect values
:( test_bank catches bank.py without case-insensitivity
    expected exit code 1, not 0
:) test_bank catches bank.py not allowing for entire phrase

I really dont understand what I need to do to fix my code, pytest is working fine but the actual check for the problem fails the fourth condition

Share Improve this question asked Mar 28 at 0:40 Cyrus ValiCyrus Vali 372 bronze badges 2
  • 2 Your value("Hi") test can never actually generate an error; it might (or might not) print a message, but execution continues in any case. I'm not sure why you wrote that test differently than the rest - just test that the expression has the proper value (which appears to be 100). – jasonharper Commented Mar 28 at 0:52
  • @jasonharper thank you, I tweaked the code and it worked (btw the correct value is supposed to be 20 not 100) you were right about the last test thanks! – Cyrus Vali Commented Mar 28 at 1:02
Add a comment  | 

1 Answer 1

Reset to default 0

BTW the code I got working (special thx to @jasonharper):

from bank import value


def test_greeting_charge():
    assert value("hello") == 0
    assert value("hi") == 20
    assert value(" ") == 100
    assert value("test") == 100
    assert value("hi where is 100th st?") == 20
    assert value("oloo") == 100
    assert value("Hi") == 20


if __name__ == "__main__":
    main()

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论