我的断言在python中的这段代码失败了

Yas*_*vee 1 python assert

def test_string_membership():
    assert False == 'c' in 'apple'
    assert True == 'a' in 'apple'
    assert True == 'app' in 'apple'
Run Code Online (Sandbox Code Playgroud)

ps: - 我是python的初学者,无法找出错误.当我运行代码时,我的断言失败了.

fal*_*tru 5

False == 'c' in 'apple' 不解释为

False == ('c' in 'apple')
Run Code Online (Sandbox Code Playgroud)

但,

(False == 'c') and ('c' in apple)
Run Code Online (Sandbox Code Playgroud)

因为比较链.


为了得到你想要的东西,明确地加上括号.

False == ('c' in 'apple')
Run Code Online (Sandbox Code Playgroud)

或更优选使用in/ not in:

def test_string_membership():
    assert 'c' not in 'apple'
    assert 'a' in 'apple'
    assert 'app' in 'apple'
Run Code Online (Sandbox Code Playgroud)