n = input() list = list(n) s = 0 for i in range(len(list)): s += int(list[i]) print(s)
时间: 2024-05-29 12:12:30 浏览: 102
(C语言)输入n,统计0~n中1的个数。
5星 · 资源好评率100%
This code takes an input string and converts it to a list of individual characters. It then iterates through each character in the list and converts it to an integer, storing it in the variable "s". However, the variable "s" is overwritten with each iteration, so it only stores the last integer in the list.
To fix this, you could add each integer to a sum variable instead of storing it in "s". Here's the corrected code:
n = input()
lst = list(n)
sum = 0
for i in range(len(lst)):
sum += int(lst[i])
print(sum)
This code will iterate through each character in the list and add its integer value to the sum variable. At the end, it will print the sum of all the integers in the input string.
阅读全文