Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在制作一个不同的程序,在这种情况下,如果变量中有任何来自变量A的字符串,B它应该打印正常
A
B
A = "hello", "nice" B = "this dress is very nice" if A in B : print("ok")
我收到错误 -
[Done] exited with code=1 in 0.183 seconds
关于您的要求
如果变量A中的变量有任何字符串B
那应该是:如果其中有一部分A等于B,则对应的代码是
A = "hello", "nice" B = "nice" if B in A: print("ok")
如果您想要一个可能的部分匹配:一个单词A包含在B,请使用:
A = "hello", "nic" B = "nice" if any(word in B for word in A): print("ok")