https://pythonmonk.com/
定义函数计算传入参数中数字的个数
def count_digits(n):
"""Counts the number of digits in the given number.
>>> count_digits(5)
1
>>> count_digits(42)
2
>>> count_digits(9876543210)
10
>>> count_digits(2 ** 100)
31
"""
# your code here
tempstr=str(n)
count=0
for i in range(0,len(tempstr)):
if tempstr[i]<='9' and tempstr[i]>='0':
global count
count=count+1
return count
在首次写法中,丢失global count 使的函数内部count=count+1 出现语法错误
本文介绍了一个Python函数,该函数能够计算给定整数中的数字个数,并提供了几个示例来展示其使用方法。文章还指出了一处常见的错误:在函数内部未正确声明变量导致的问题。

725

被折叠的 条评论
为什么被折叠?



