-1

我正在尝试将整数更改为字符串,然后再返回整数,因为它重复到 100。例如,我有一个倍数和五个数,它需要求和然后像 print(multnum+"x5="+答案)为此,我必须将其转换为字符串。这个过程使用 while 函数重复,所以为了使用 multnum 进行另一个求和,它必须返回一个整数。

def output100_5table():
    answer = 0
    thefive = 5
    multnum = 0
    addmult = multnum+1
    thetimes = "x5="
    while answer < 100:
        addmult = int(multnum+1)
        answer = addmult*thefive
        addmult = str(addmult)
        answer = str(answer)
        print(addmult+thetimes+answer)
output100_5table()
4

1 回答 1

1

这是你想要的?很难弄清楚代码的目的是什么。

>>> def show_table(multiplicand, product_limit):
    multiplier = 1
    while True:
        product = multiplicand * multiplier
        if product > product_limit:
            break
        print(multiplicand, 'x', multiplier, '=', product)
        multiplier += 1


>>> show_table(5, 100)
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
5 x 11 = 55
5 x 12 = 60
5 x 13 = 65
5 x 14 = 70
5 x 15 = 75
5 x 16 = 80
5 x 17 = 85
5 x 18 = 90
5 x 19 = 95
5 x 20 = 100
>>> def show_table(multiplicand, product_limit):
    for multiplier in range(1, product_limit // multiplicand + 1):
        print(multiplicand, 'x', multiplier, '=', multiplicand * multiplier)


>>> show_table(5, 100)
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
5 x 11 = 55
5 x 12 = 60
5 x 13 = 65
5 x 14 = 70
5 x 15 = 75
5 x 16 = 80
5 x 17 = 85
5 x 18 = 90
5 x 19 = 95
5 x 20 = 100
>>> 
于 2013-07-03T20:33:16.923 回答