【发布时间】:2012-11-01 02:41:59
【问题描述】:
我对编程相当陌生,并且一直在开发一个程序来计算从 0 到 9 的每个值在数字字符串中出现的次数(该程序必须使用调用它的函数和主函数)。如果用户输入数字 123512378,我希望它告诉我 1 出现 2 次 2 出现 2 次...... 8 出现 1 次等等。现在我正在尝试将一个字符串传递给一个函数,然后返回一个带有数字的列表发生的顺序。但是我的只是返回我在开始时生成的空列表。这是我的代码:
def countdigits(aString,Result):
countValue=0
while countValue>=9:
Result[countValue]=(aString.count(str(countValue)))
countValue=countValue+1
return Result
def main():
emptyList = 10 * [0]
numbers=str(input("Pleas enter a string of numbers: "))
print(countdigits(numbers,emptyList))
main()
【问题讨论】:
标签: python algorithm python-3.x