How to print int as words seq in python

Viewed 64

I have a table with words numbers

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

And the user should wrote a number in input:

number = input()

Now I need to create function printing word representation in that way (eg. for 242):

> 242 - two four two

I wrote that code but it's not working propetly (problem with indexes or printing list). How can I do that function in esiest way?

My tried code:

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 Answers

Try this:

table=['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
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=' ')

OUTPUT:

684
six eight four

586493
five eight six four nine three
Related