unorderable类型:str()< int()不能将int更改为string到int - Python

时间:2013-07-03 16:07:45

标签: python python-3.x type-conversion

我试图将一个整数更改为字符串然后返回整数,因为它重复到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()

1 个答案:

答案 0 :(得分: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
>>>