【问题标题】:unorderable types: str() < int() cannot change int to string to int - Python [closed]不可排序的类型:str() < int() 不能将 int 更改为 string 到 int - Python [关闭]
【发布时间】:2013-07-03 16:07:45
【问题描述】:

我正在尝试将整数更改为字符串,然后再返回整数,因为它重复到 100。例如,我有一个倍数和五个数,它需要进行总和然后像 print(multnum+"x5 ="+answer) 为此,我必须将其转换为字符串。这个过程使用 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()

【问题讨论】:

  • 所以answer 是一个字符串,你不能将它与一个int(在你的情况下为100)进行比较。你有什么问题 ?只需将其转换为 int。
  • 或者停止将其转换为str。

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


【解决方案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
>>> 

【讨论】:

  • 谢谢你,这正是我想要的!!!
猜你喜欢
  • 2017-05-08
  • 2013-01-30
  • 1970-01-01
  • 2014-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多