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

时间:2015-06-15 19:14:36

标签: python list

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'不可迭代。

1 个答案:

答案 0 :(得分:1)

您需要构建这些数字的列表。相反,您只需将每个数字指定给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)