Python IDE冻结错误,关于函数的问题
为什么每次我搬家, obj/=10。在第8行,当我运行此代码时,IDE冻结Python IDE冻结错误,关于函数的问题,python,python-3.x,algorithm,sum,Python,Python 3.x,Algorithm,Sum,为什么每次我搬家, obj/=10。在第8行,当我运行此代码时,IDE冻结 def print_sum(obj): s = 0 if isinstance(obj, int): while obj: s += obj % 10 obj //= 10 print('Sum of digits:', s) print_sum(12345) 我能知道obj/=10的确切用途吗? 你能解释一
def print_sum(obj):
s = 0
if isinstance(obj, int):
while obj:
s += obj % 10
obj //= 10
print('Sum of digits:', s)
print_sum(12345)
我能知道obj/=10的确切用途吗?
你能解释一下这个函数会发生什么吗?因为如果不改变obj,你就会陷入无限循环。
obj//=10
整数除以obj,直到它为0,循环结束。写obj=obj//10
代码冻结,因为您不更新obj
,因此不退出while