python - 如果在字符串中找到字符串,则无法更新变量

标签 python

我正在尝试使用 for 循环迭代更新名为 status 的变量

status = 'The device1 is present'

devices = ['device1', 'device2', 'device3']

for device in devices:
    if device not in status:
        status = 'fine'

print(status)

我得到的输出是'fine',而不是'The device1 is present'

尝试了多种方法都无法解决。

最佳答案

如果 devices 中只有一项在 status 中不存在,它将更改为“fine”,并且您有两项(“device2”和“device2”) 'device3'),但 status 中不存在,因此您将以值为 'fine' 的 status 结尾。

关于python - 如果在字符串中找到字符串,则无法更新变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54135612/

相关文章:

python - 如何使用数组输入调用 python scipy quad

python - 在数据框中创建变量作为另一个数据框中其他变量和值的函数的快速方法?

python - 用户电子邮件(正确)在修改后的 firenotes 中显示为在前端验证但后端未验证

python - 如何通过将列的值与没有迭代的字符串进行比较来获取数据帧的索引

Python-从字典列表创建动态嵌套字典

python - 如何在 Python 脚本中创建单独的命名空间?

python - 如何在 mpi4py 中分散和收集 python 对象列表

python - Robot Framework - 将 Appium 驱动程序传递给 python 脚本

python - 带参数执行curl - Python 2.6.6

python - 为给定的单词集创建 id 的简单 [pythonic] 方法