我正在尝试编写一些代码,如果发生某种情况,则必须将特定数字更改为 -that 数字。 到目前为止,我有以下代码:
x=6
for words in foo:
if "bar" in words:
crazy_function(x)
else:
pass
如果单词“bar”在单词中,x 需要输出为 -6,如果不在单词中,则需要输出为 +6。 然而,在某些情况下,x=-6 在这种情况下,如果 bar 在文字中,则它需要变为正数。 我需要用实际有效的东西替换“crazy_function()”。
请您参考如下方法:
使用否定运算符:
x=6
for words in foo:
if "bar" in words:
x = -x;
