程序读取数字n并计算n + nn + nnn:答案继续以零加

时间:2017-10-26 22:03:25

标签: python

强文#计划解释了累计相同的数字添加,但问题在于         回答额外的零点

    a = int(input("enter the number of units you want")) #number of times
    b = input("Enter the digit") #actual integer
    ans = str(0)
    z = 0
    for i in range(1,a+1):    #loop for number of integers
        for j in range(1):    #loop for repitation of integer
            ans=b+ans
        z = int(ans)+int(z)
    print("Ans is"+" "+str(z))

2 个答案:

答案 0 :(得分:0)

你几乎是正确的。不需要range(1)循环,ans应该从0开始为空。

a = int(input("enter the number of units you want")) #number of times
b = input("Enter the digit") #actual integer
ans = ""
z = 0
for i in range(1,a+1):    #loop for number of integers
    ans=str(b)+ans
    z = int(ans)+int(z)
print("Ans is"+" "+str(z))

答案 1 :(得分:0)

假设,对于数字6输入5次,您需要输出:

"66666"

然后只需更改

ans = str(0) to  ans = "" or ans = ''

否则,如果输出应为30,则需要:

...
b = int(input(...))
ans = 0
...