gpt4 book ai didi

python - 将整数转换为负数

转载 作者:太空宇宙 更新时间:2023-11-04 06:44:52 25 4
gpt4 key购买 nike

我正在尝试编写一些代码,如果发生某种情况,则必须将特定数字更改为 -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;

关于python - 将整数转换为负数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17711004/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com