my question here
my code here
How is to use desired patterns programming ? Such as :
case:
....int 20 + int 1 : print('twenty') + print('one')
....int 20 + int 7 : print('twenty') + print('seven')
tysm in advance.
I don't understand your question. Do you want to print 'twenty-one' when you get the number 21?
You will need a couple of dictionaries - one for values, one - for powers of 10, like
{1: 'one',
2: 'two',
....
11: 'eleven'
...
90: 'ninety'}
and
{10: 'ten',
100: 'hundred'
1000: 'thousand'
}
And then you'll have to parse digits in the reversed order with some convoluted logic thrown into the fray
It's okay to write for a limited range.
To make it work for big range,it's a lot of work.
There are libraries for this like
inflect.
Test:
pip install inflect
>>> import inflect
>>> p = inflect.engine()
>>> p.number_to_words(21)
'twenty-one'
>>> p.number_to_words(27)
'twenty-seven'
# Big number
>>> p.number_to_words(12345)
'twelve thousand, three hundred and forty-five'
>>> p.number_to_words(956748)
'nine hundred and fifty-six thousand, seven hundred and forty-eight'
# Group
>>> p.number_to_words(21, group=1)
'two, one'
>>> p.number_to_words(965495, group=1)
'nine, six, five, four, nine, five'
>>> p.number_to_words(965495, group=2)
'ninety-six, fifty-four, ninety-five'
Up to 99, you can do it with two lists:
under_twenty = ['', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', ..., 'nineteen']
twenty_up = ['twenty', 'thirty', 'forty', ..., 'ninety']
Then you can use numeric indexing (divided by ten for twenty_up, hint: use //), and only have three cases to worry about: 0, < 20, >= 20.