我对编码很陌生,我正在尝试编写一个 python 脚本,其中用户输入一个整数,并以扩展形式显示该整数,该整数提高到 10 的幂。

示例:用户输入 643541,脚本输出 643541 = (6x10^5 )+(4x10^4)+(3x10^3)+(5x10^2)+(4x10^1)+(1x10^0)
这是我的代码

A = [7000, 400, 70,1]
cond = True
y = 0
i = 0
sizeArray = len(A)
for i in range(0, sizeArray-1):
    while cond == True:
        if A[i]%10 == 0:
            A[i] = A[i]/10
            y += 1

        else:
            cond = False
    print(y)

我尝试使用示例数组来测试零的数量,但我不知道如何输出如上的结果。

我怎样才能做到这一点?

最佳答案

您可以将输入的整数 643541 转换为数字数组 [6,4,3,5,4,1] 。然后为指数维护一个变量。它将为数组中的每个数字递减

def function(num):
 digits = str(num) # convert number to string
 output = []
 for i, digit in enumerate(digits):
   output.append("(" + digit + "x10^" + str(len(digits)-i-1) + ")")
 return " + ".join(output)

这里 len(digits)-i-1 起到了维护指数值的变量的作用

关于Python- 数字的扩展形式的十次方,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59330621/

10-14 18:11
查看更多