我正在尝试使用 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/