I am working on a function assignment, and I am very new to Python. I need to write a program that converts numbers in to words "Spelling(205)" would become "Two Hundred Five." My instructor recommends using spelling(calling spellThreeDigits() twice) and spellings(calling spellThreeDigits() three times) in my function. I do not know what she means by doing this. Can someone please explain?
spelling() and spellThreeDigits() are functions, small sections of code that do a small task. This is a fairly challenging assignment for a basic course.
No kidding! :) That's exactly what I was thinking, (and why I have the angry user name). Basically what I have is a dictionary that will contain the keys and values, so (6:six) or (10: ten), then I will need to use "if else" statements and the above mentioned functions. I cannot use more than 30 lines of either. Someone else mentioned recursion but now the instructor is saying that I only need a few extra "if checks" on the size of the number, (millions, thousands). I was thinking that I would then use slice, modify or append strings but I'm not entirely sure if that would work. The number range also has to be -1 billion to 1 billion, but essentially the minus sign just needs to go from user input as "-" to print in the output as "minus." That's an 'if" statement I think. I'm basically confused on how this will all tie in together and the "mini" functions have me very confused.
I was thinking about this last night, and I'm not sure that a dictionary is the best choice. Four lists: power, tens, teens and ones, may be better: power = [ '', '', "hundred", "thousand", "million", "billion" ] tens = [ '', "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" ] teens = [ "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" ] ones = [ "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" ] Grouping the digits into groups of three is probably needed, and the individual digits can index the appropriate list to get the correct word which can be used to concatenate an answer string.
Yes, I was initially thinking the same thing, but then someone from my class said it was best to create just one list or directory, and then the instructor stated we would not be using exponential functions in this assignment. This was my post to her: " This assignment was deceptively challenging. I think its the number range that is making it difficult to complete. Since it is between "0" and 1 billion, it is not like you can just append a string or a list like: def addTo(list): list.append(4) Unless that is the case then this assignment is just extremely labor intensive and there isn't enough time to complete it. Also, you can't just raise "num" exponentially: def raiseToPower(base, exp): total = 1 for count in range(exp): total = total * base return total And I'm still not sure how this would come in to play with this function?: random.randint(1,10)" And this was her response: "Though you didn't ask any questions, and you didn't ask for a hint, I will give you some additional information about the assignment: The range requirement is not between 0 and a billion, negative numbers must be dealt with also. The random number generator is not useful for this assignment. The power function is also not useful in this case. The trick is to start by writing a function that spells out any number between 1 and 999. Then you notice that six digit numbers will use those same spellings (calling spellThreeDigits() twice), just with the word "thousands" stuck in the middle. Then you notice that nine digit numbers will also use those same spellings(calling spellThreeDigits() three times), just with the word "millions" stuck in the middle. So the only difficult function to write is the one that spells out any number between 1 and 999."
I realize that. Don't take my names as gospel, those lists are just conceptual. The fun part will be writing the 0-999 part, but the items in the "power" list will still come into play to make a full pronunciation of numbers up to one billion. Actually, she gave you a good framework for doing this assignment. Also remember, I'm supposed to help you, not give you the answer, so my replies are constrained.
Thank you. I did finally figure it out. Really a tough assignment.
Congrats! It looked like a fun one.
Join our real-time social learning platform and learn together with your friends!