为什么用逻辑正确的python3来计算数字?
我在编辑器中编写了以下代码:为什么用逻辑正确的python3来计算数字?,python,python-3.x,logic,Python,Python 3.x,Logic,我在编辑器中编写了以下代码: #!/usr/bin/env python3 def digits(n): count = 0 if n == 0: count = 1 while n > 0: n /= 10 count += 1 return count print(digits(25)) # Should print 2 print(digits(144)) # Should print 3 print(d
#!/usr/bin/env python3
def digits(n):
count = 0
if n == 0:
count = 1
while n > 0:
n /= 10
count += 1
return count
print(digits(25)) # Should print 2
print(digits(144)) # Should print 3
print(digits(1000)) # Should print 4
print(digits(0)) # Should print 1
但我越来越喜欢:
325
326
327
1
这是错误的逻辑还是,我在这方面错了什么?使用/=
进行整数除法
def digits(n):
count = 0
if n == 0:
count = 1
while n > 0:
n //= 10
count += 1
return count