''' Software Tools Exam 2 Question 2: Write a program to convert numbers to letters and output the sum of the letters from 0 to 999. For example, 342 should be converted to "three hundred forty two", then sums the letters of each word ---> three hundred forty two = 5 + 7 + 5 + 3 = 20 ''' import sys def main(): # Define output array output_lst = ["", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"] ten_lst = ["ten", "eleven", 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'] tens_lst = ['','','twenty','thirty','forty','fifty','sixty','seventy','eighty','ninety'] hundreds_lst = ['hundred'] out_string = ""; sumed = 0 # Define counter var to get place (ones, tens, hundreds) counter = 0; # Define list var to store values char_lst = [] # Iterate over values for input_val in range (0,1000): # Print current iteration print '--------------------------------------------------' print 'INPUT VAL: %d ' % (input_val) # Reset counter var counter = 0 # Check for zero: if (input_val == 0): # Zero's value is 4, so just add 4 and keep going print 'Zero => sum 4' sumed +=4 continue # Check for additional tens spots while (input_val / 10.0 >= 1): # Increment counter (One additional spot found) counter+=1 # Append the last digit to the list char_lst.append(input_val % 10) input_val = input_val/10 # Get total tens spots (stored in counter) counter+=1 # Get final array element char_lst.append(input_val % 10) # Reverse array so it's in standard order (hundreds,tens,ones) char_lst.reverse() print char_lst for x in char_lst: # Hundreds place first if counter == 3: # The lists are specially formatted such that the index of the number is the string value of the number. Index[3] = three, hundreds[0] = 'hundred' out_string += (output_lst[x] + ' ' + hundreds_lst[0]+' ') # Add string size to running total and print sumed += len(output_lst[x] + hundreds_lst[0]) print "%s %s sum %d" % (output_lst[x], hundreds_lst[0], sumed) # Tens place next elif counter == 2: # Check for teens spot with some tricky conversions, and check for length so we don't get an index error if len(char_lst) == 3: # Check if number is a teen if int(str(char_lst[1]) + str(char_lst[2])) in range(10,20): # Use special teen list to print result, and break so the ones digit doesn't reprint sumed += len(ten_lst[char_lst[2]]) out_string += ten_lst[char_lst[2]] print "%s, total sum %d" % (ten_lst[char_lst[2]], sumed) counter = 0 break # Easier to program in an if statement than it is to retake modulus and make it all one case elif len(char_lst) == 2: if int(str(char_lst[0]) + str(char_lst[1])) in range(10,20): # Use special teen list to print result, and break so the ones digit doesn't reprint sumed += len(ten_lst[char_lst[1]]) out_string += ten_lst[char_lst[1]] print "%s, total sum %d" % (ten_lst[char_lst[1]], sumed) counter = 0 break # Else it's not a special teen, so just perform as normal out_string += (tens_lst[x] + ' ') sumed += len(tens_lst[x]) print "%s, total sum %d" % (tens_lst[x], sumed) # Ones place final else: # Index[x] = long_string(x) out_string +=(output_lst[x]) sumed += len(output_lst[x]) print "%s, total sum %d" % (output_lst[x], sumed) counter -= 1 # Clear number list on every iteration char_lst = [] # Print final result print "total sum is %d" % (sumed) # Repeat loop for next input # boiler plate code if __name__ == '__main__': main()