HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Numbers to Text Program - Python Training

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
numberstextprogramtrainingpython

Problem

I have written a numbers to text challenge for some people in my organisation to practice their Python skills. I am an economist working with pandas, but I am trying to teach them some stuff about general programming as well. I am not a trained computer scientist or anything, so what I know is just what I have picked up along the way.

I have written a solution to the numbers to text problem and would like feedback on the following:

  • Any general comments about the functions. I find the tuple to text function to be a bit scary to look at.



  • Any suggestions as to how to write helpful comments. Squeezing them in next to lines of code themselves seems to confuse the reading, but creating new lines makes the function look


massive and it can be hard to read - suggestions welcome.

  • Any other possible systems that are simple to implement.



Incidentally, solutions deliberately do not make use of user defined classes. A full description of the problem and the method I used to solve it is provided:

```
##########
##### DESCRIPTION #####
##########
""" The task is to write a number to text program that can convert any number between
minus 2 billion and plus 2 billion. It sounds deceptively simple...

Examples:
10 -> ten
121 -> one hundred and twenty one
1032 -> one thousand and thirty two
11143 -> eleven thousand one hundred and forty three
1200011 -> one million two hundred thousand and eleven

Note: if you ever want to turn a nested list into a single list containing all the
elements of the nested lists, use itertools.chain()

e.g.
In [1]: l = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
In [2]: l2 = list(itertools.chain(*l))
In [3]: l2
Out[4]: [1, 2, 3, 4, 5, 6, 7, 8, 9]

The solution provided here is based upon the following:

+ If the length of a st

Solution

I will review a single function the get_number().

-
From what I have seen in your code there is completely no benefit of passing messages as variables message1 and message2. It is easer to read code which looks like this:

user_input = raw_input("Please enter a number between -2 billion and 2 billion: ")


If you still decide to pass it as variables you should give a descriptive name to your message variable

user_input = raw_input(msg_ask_to_enter_value)


-
Recursion will reach limits and will throw an exception if I keep entering wrong values. It's not a serious issue, as I would need to repeat for 1000 times but still its better not to leave this bug especially when it is so easy to get around it. Instead of recursion use a loop:

def get_number():
    boundary_low = -2000000000
    boundary_high = 2000000000

    while True
        input = raw_input('Please inter a number in range of %s to %s: ' % (
                           boundary_low, boundary_high))
        try:
            value = int(input)
        except:
            print 'That was not a number'
        else:
            if boundary_low <= value <= boundary_high:
                return value
            else:
                print 'Number out of boundaries'

Code Snippets

user_input = raw_input("Please enter a number between -2 billion and 2 billion: ")
user_input = raw_input(msg_ask_to_enter_value)
def get_number():
    boundary_low = -2000000000
    boundary_high = 2000000000

    while True
        input = raw_input('Please inter a number in range of %s to %s: ' % (
                           boundary_low, boundary_high))
        try:
            value = int(input)
        except:
            print 'That was not a number'
        else:
            if boundary_low <= value <= boundary_high:
                return value
            else:
                print 'Number out of boundaries'

Context

StackExchange Code Review Q#46273, answer score: 2

Revisions (0)

No revisions yet.