Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Nancy Melucci
Courses Plus Student 36,159 PointsMore Python Homework Help
OK gang, you really helped me last time AND I hope I am not abusing your kindness. I'm learning Python and Java right now. I struggle much more with Python. But I am sticking to both.
I have to write a program that turns numeric numbers into word versions of the same (so, it spells out numbers) up to 9 digits positive and negative (-999,999,999 to 999,999,999). No commas. Again, I don't want anyone to write to code for me, but I need help getting started. SO: hints at good sites to read about and pick up tips on how to create such functions, etc. The main function needs to be 30 lines or under, there can be helper functions AND the whole program can be more than 30 lines.
Thanks for any small crumbs of information you can send my way. I was about to give up on this. I really don't want to. I love coding and believe that pushing passed the point where one feels that one's skills may be inadequate is one of the things that makes that happen. That and practice, practice practice.
Nancy M.
5 Answers
Patrick Gerrits
14,614 PointsYou're saying that you want to have a number like 245 and then that the program gives back two hundred forty five?
If you want that, i suppose you need a dictionary that writes all these combinations, or an algorithm. Like there are 3 digits, so that means hundred. The first number is 2 so two hundred then there are 2 numbers left (45) that means that it is not four five, but forty 5 then you have 5 left and translate that to five.
Still you need to have a dictionary with all the uniqe combinations (one to twenty, thirty, forty,..., hundred, thousand, ten thousand, etc etc )
Nancy Melucci
Courses Plus Student 36,159 PointsI got it to work. So I thought I would share it with you. Thanks again. I love having Treehouse as a resource... Thank you and have a good week.
#a function to spell out numbers in words
def spellnum(num,join=True):
#Lists of number words
thousands = ['','thousand','million']
tens = ['','ten','twenty','thirty','forty','fifty','sixty','seventy', \
'eighty','ninety']
teens = ['','eleven','twelve','thirteen','fourteen','fifteen','sixteen', \
'seventeen','eighteen','nineteen']
units = ['','one','two','three','four','five','six','seven','eight','nine']
#Empty List for number words
words = []
#zero case
if num==0: words.append('zero')
#how to handle negative numbers
if num < 0: words.append('negative')
num = abs(num)
#a series of steps to process the numbers and turn them into words
numStr = '%d'%num
numStrLen = len(numStr)
groups = (numStrLen+2)/3
numStr = numStr.zfill(groups*3)
for i in range(0,groups*3,3):
h,t,u = int(numStr[i]),int(numStr[i+1]),int(numStr[i+2])
g = groups-(i/3+1)
if h>=1:
words.append(units[h])
words.append('hundred')
if t>1:
words.append(tens[t])
if u>=1: words.append(units[u])
elif t==1:
if u>=1: words.append(teens[u])
else: words.append(tens[t])
else:
if u>=1: words.append(units[u])
if (g>=1) and ((h+t+u)>0): words.append(thousands[g]+',')
#final joining of parts
if join: return ' '.join(words)
return words
#sample calls
print spellnum(0)
print spellnum(10)
print spellnum(120000)
print spellnum(-90902)
#Recording of Run
%run C:/Users/Nancy/Desktop/SelfStudyMaterials/draftassign4.py
zero
ten
one hundred twenty thousand,
negative ninety thousand, nine hundred two
End
#drnanjo author
#with generous help from: Mr.Regna, Prof. Haight, Stack Overflow and Treehouse
Kenneth Love
Treehouse Guest TeacherI see a few solutions here.
But, yeah, I'd probably start where Patrick Gerrits suggested. Find out how many sets of numbers you have to work with (hundreds, thousands, ten thousands, etc) and then start turning the leftmost number into the word version of it. Then deal with converting tens places if needed, and finally use the word for the last number and attach the place (millions, etc).
Nancy Melucci
Courses Plus Student 36,159 PointsHere's the assignment:
"Write a function that takes a whole number as parameter and returns a string containing the number spelled out in English. For example, the function call: spell (123) will return the string: one hundred twenty three Your function must work for any whole number whose value is between -999,999,999 and 999,999,999. Of course, the numbers will not have commas in Python. In order to save time, work through your program on paper before attempting to write it in Python. Start by thinking of helpful functions that can assist you in writing your spell() function. Make sure that you write and test just ONE function at a time! In order to receive full credit: 1) No function can be longer than 30 lines. See program guidelines 2) Your program can have no duplicate code. 3) You must use the modulus operator (%). 4) Every function must have a comment telling what the function does with its parameter(s) and what (if anything) it returns. Hint: You might find this website useful in telling what the returned values from your spell() function should look like: http://www.englisch-hilfen.de/en/texte/ausschreiben.htm"
I cannot get the widget at the link to work by the way. Not helpful.
Kenneth Love
Treehouse Guest TeacherExcellent work Nancy Melucci! Glad to see you stick it out and work through it.
What seems to be your sticking point with Python?