代码之家  ›  专栏  ›  技术社区  ›  martin

如何在python中将int打印为words seq

  •  1
  • martin  · 技术社区  · 5 年前

    我有一张写数字的桌子

    table = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten']
    

    用户应该在输入中写入一个数字:

    number = input()
    

    现在我需要以这种方式创建函数打印单词表示(例如,对于242):

    > 242 - two four two
    

    我写了那段代码,但它不能正常工作(索引或打印列表的问题)。我怎样才能用最简单的方式来实现这个功能呢?

    我的密码:

    table=['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten']
    number = input()
    
    for x in range(len(table)):
        print(table[number[x]], end=' ')
    
    1 回复  |  直到 5 年前
        1
  •  4
  •   Charif DZ    5 年前

    试试这个:

    table=['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten']
    number = input()
    
    # loop over the input string to get number by number
    for x in number:
         # now get the string representation from its index in table
        print(table[int(x)], end=' ')
    

    输出:

    684
    six eight four
    
    586493
    five eight six four nine three