gpt4 book ai didi

python3 除法运算符与 python2 完全一样

转载 作者:行者123 更新时间:2023-11-28 21:54:35 25 4
gpt4 key购买 nike

我(真的,真的)需要除法运算符在 Python3 中表现得像在 Python2 中一样。

Python2代码:

--> 11/5
2
--> 11.0/5
2.2

但是在 Python3 中我们有

--> 11//5
2
--> 11.0//5
2.0

我可以将/更改为//或其他任何内容,但我希望得到相同的结果。有任何想法吗?

最佳答案

你不能让它像那样工作。您必须在适当的时候使用 ///

如果出于某种原因,您需要旧运算符的“多态性”,您可以...

def div(a, b):
if isinstance(a, int):
return a // b
else:
return a / b

关于python3 除法运算符与 python2 完全一样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23949247/

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