3 回答

TA贡献1865条经验 获得超7个赞
你可以把它变成一个字符串,然后分解它。例如:
number = 11
[int(c) for c in str(number)]
或者你可以用整数除法和模数的数学方式来做:
number // 10, number % 10
这两个都给你一个两个 1 的序列。
如果我了解您要正确执行的操作,则可以尝试以下操作:
sum(n//10 + n%10 for n in range(1, sums + 1))
当是 12 时给出 51。sums如果你想接受更多的数字,你还必须添加n // 100etc。

TA贡献1809条经验 获得超8个赞
number = int(input("Please input a number: "))
sum = 0
counter = 0 # will count from 1-9, and reset if it goes too far
for n in range(number):
if counter > 9:
counter = 1 # limit the counter
sum += counter
这将计数到您输入的数字,它将存储由计数器管理的 1-9 序列的总和。

TA贡献2036条经验 获得超8个赞
希望这是您正在寻找的,
sums = int(input("Enter page sum: ")) #Get the input
lst=map(str,list(range(1,sums+1))) #Map int list to string
lst_concat = ''.join(lst) #Merging the elements in the list together
lst2=list(lst_concat) #Make it into a list again
sum(map(int,lst2)) #Sum the elements of the digit
添加回答
举报