One More Example

I think another example method would be helpful here. We’ll call this one english_number. It will take a number, like 22, and return the English version of it (in this case, the string 'twenty-two'). For now, let’s have it work only on integers from 0 to 100:

def​ english_number number
# We accept numbers from 0 to 100.
if​ number < 0
return​ ​'Please enter a number zero or greater.'
if​ number > 100
return​ ​'Please enter a number 100 or less.'
num_string = ​''​ ​# This is the string we will return.
# "left" is how much of the number
# we still have left to write out.
# "write" is the part we are
# writing out right now.
# write and left... get it? :)
left = number
write = left/100 ​# How many ...

Get Learn to Program, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.