你可以把它变成一个字符串,然后分解它。例如: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。
number = int(input("Please input a number: "))sum = 0counter = 0 # will count from 1-9, and reset if it goes too farfor n in range(number): if counter > 9: counter = 1 # limit the counter sum += counter这将计数到您输入的数字,它将存储由计数器管理的 1-9 序列的总和。
希望这是您正在寻找的,sums = int(input("Enter page sum: ")) #Get the inputlst=map(str,list(range(1,sums+1))) #Map int list to stringlst_concat = ''.join(lst) #Merging the elements in the list togetherlst2=list(lst_concat) #Make it into a list againsum(map(int,lst2)) #Sum the elements of the digit