python - 我对 python 中的这段代码断言失败

标签 python assert

def test_string_membership():
    assert False == 'c' in 'apple'
    assert True == 'a' in 'apple'
    assert True == 'app' in 'apple'

p.s:- 我是 python 的初学者,无法找出问题所在。当我运行代码时,我的断言失败了。

最佳答案

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

False == ('c' in 'apple')

但是,

(False == 'c') and ('c' in apple)

因为 comparison chaining .


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

False == ('c' in 'apple')

或更优选地使用in/not in:

def test_string_membership():
    assert 'c' not in 'apple'
    assert 'a' in 'apple'
    assert 'app' in 'apple'

关于python - 我对 python 中的这段代码断言失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20943854/

相关文章:

python - 将工作目录设置为笔记本目录

python - 我在哪里可以找到发布 GIL 的 numpy 函数列表?

python - 如何让玩家/敌人被击中时改变颜色?

python - 安装python包时出现错误: pythonw not found

Python 单元测试 : AttributeError: no attribute 'assertTrue' when running on Linux

python - 使用python诱变剂后,未显示元数据

php - assertEquals 不比较数组值类型?

Python 输入有效性和使用断言

java - 在Java中断言一个集合有一个项目的多个实例?

java - 用于测试 Java 类结构的库或框架