添加while循环python中创建的数字

2024-10-16 17:28:06 发布

您现在位置:Python中文网/ 问答频道 /正文

repeat1=0 
while repeat1!=x1:
    fini=ord(dlist[repeat1])
    repeat1=repeat1+1
    print (fini)
sum_of_all=sum(fini)
print(sum_of_all)

我想把变量fini的数字相加。 python给了我一个错误:“int”不是iterable。你知道吗


Tags: of错误数字alliterableintsumprint
1条回答
网友
1楼 · 发布于 2024-10-16 17:28:06

你需要建立一个这些数字的列表。相反,您只是将每个数字分配给fini,而不使用前面的数字:

values = []
while repeat1 != x1:
    fini = ord(dlist[repeat1])
    values.append(fini)
    repeat1 = repeat1 + 1

sum_of_all = sum(values)

不过,您也可以对循环中的值求和:

sum_of_all = 0
while repeat1 != x1:
    fini = ord(dlist[repeat1])
    sum_of_all += fini
    repeat1 = repeat1 + 1

print(sum_of_all)

相关问题 更多 >