在Python中无法将大整数除以10并舍去小数位数

问题描述

以下程序遇到错误,因为当我将大整数除以10并截断它时,Python进行了奇怪的数学运算并返回了另一个数字。

# Program to add the digits in an integer
# eg. 463728 would equal 30 ( 4+6+3+7+2+8 )

the_sum = 2**1000

running_tot = 0
sum_length = len(str(the_sum)) # to calculate the length of the integer

i = 0 #initialize iterator

while i < sum_length:
    remainder = the_sum % 10     # Find out the last no. in int.
    running_tot += remainder     # adds the last no. to running total
    the_sum = int(the_sum / 10)  # truncates the last no. for original number
    i += 1                       # increment iterator 
 
print(running_tot)

我想知道为什么Python要这么做吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)