gpt4 book ai didi

python - Python 中的数字出现计数器

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:38:10 25 4
gpt4 key购买 nike

我对编程相当陌生,一直在开发一个程序来计算 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()

最佳答案

使用 while countValue<=9: .您使用了 >= .

关于python - Python 中的数字出现计数器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13163767/

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