# Lab Acitivtiy 4

May 5th, 2022
801
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. import random
2.
3. # Variables for the words
4. numstring = ["", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"]
5. # Variation of the first numstring that is used for one-digit numbers
6. numstringZero = ["Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"]
7. numstringTens = ['', '', 'Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety']
8. numstringOnes = ['Ten', 'Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen',
9.                  'Nineteen']
10.
11. # Used to repeat the program until the user says to end. Uses a while loop
12. rerun = 'Y'
13. while rerun.upper() == 'Y':
14.     print('This program can convert numbers from 1-999,999,999 to TEXT. \n'
15.           'The input is of type integer, you must not enter commas.')
16.     # Input for the User
17.     option = input('Would you like the program generate a random number? Y/N: ')
18.     if option.upper() == 'Y':
19.         userInput = str(random.randint(0, 999999999))
20.     else:
21.         userInput = input("Enter a number: ")
22.
23.     # Gets the amount of numbers
24.     length = len(userInput)
25.     # Outputs the amount of digits
26.     print(f'It has {length} digits.')
27.     # message when the inputted number is too long
28.     if length > 9:
29.         print('Number is too long convert to words, please keep it to 9 digits only.')
30.
31.     # for a 1 digit input
32.     def zero(numinput):
33.         x = numstringZero[int(numinput[-1])]
34.         return x
35.
36.     # for a 1 digit input
37.     def ones(numinput):
38.         x = numstring[int(numinput[-1])]
39.         return x
40.
41.     # for a 2 digit input
42.     def tens(numinput):
43.         if int(numinput[-2]) == 1:
44.             a = numstringOnes[int(numinput[-1])]
45.         elif int(numinput[-2]) == 0:
46.             a = numstring[int(numinput[-1])]
47.         else:
48.             a = numstringTens[int(numinput[-2])] + ' ' + ones(numinput)
49.         return a
50.
51.     # for a 3 digit input
52.     def hundreds(numinput):
53.         b = numstring[int(numinput[-3])]
54.         if int(numinput[-3]) > 0:
55.             a = b + ' Hundred '
56.         else:
57.             a = b + ''
58.         return a + tens(numinput)
59.
60.     # for a 4 digit input
61.     def thousands(numinput):
62.         x = numstring[int(numinput[-4])]
63.         if int(numinput[-4]) > 0:
64.             a = x + ' Thousand '
65.         else:
66.             if int(numinput[-5]) > 0:
67.                 if int(numinput[-4]) > 0:
68.                     a = x + 'Thousand '
69.                 else:
70.                     a = x + ' Thousand '
71.             else:
72.                 if int(numinput[-0]) > 0:
73.                     if int(numinput[-6]) > 0:
74.                         a = x + 'Thousand '
75.                     else:
76.                         a = x + ''
77.                 else:
78.                     a = x + ''
79.         return a + hundreds(numinput)
80.
81.     # variation of the 4 digit input that takes into account inputted zeroes
82.     def thousandszero(numinput):
83.         if int(numinput[-4]) > 0:
84.             x = ' Thousand '
85.         else:
86.             if int(numinput[-5]) > 0:
87.                 if int(numinput[-5]) == 1:
88.                     x = ' Thousand '
89.                 else:
90.                     x = 'Thousand '
91.             else:
92.                 x = ''
93.         return x + hundreds(numinput)
94.
95.     # for a 5 digit input
96.     def tenthousands(numinput):
97.         if int(numinput[-5]) == 1:
98.             a = numstringOnes[int(numinput[-4])] + thousandszero(numinput)
99.         else:
100.             x = numstringTens[int(numinput[-5])]
101.             if int(numinput[-4]) == 0:
102.                 y = ''
103.             else:
104.                 y = ' '
105.             a = x + y + thousands(numinput)
106.         return a
107.
108.     # for a 6 digit input
109.     def hundredthousands(numinput):
110.         x = numstring[int(numinput[-6])]
111.         if int(numinput[-6]) > 0:
112.             if int(numinput[-5]) > 0:
113.                 y = ' Hundred '
114.             else:
115.                 if int(numinput[-4]) > 0:
116.                     y = ' Hundred'
117.                 else:
118.                     y = ' Hundred '
119.             a = x + y + tenthousands(numinput)
120.         else:
121.             a = x + tenthousands(numinput)
122.         return a
123.
124.     # for a 7 digit input
125.     def million(numinput):
126.         x = numstring[int(numinput[-7])]
127.         y = ' Million '
128.         return x + y + hundredthousands(numinput)
129.
130.     # for 8 digit input
131.     def tenmillion(numinput):
132.         if int(numinput[-8]) == 1:
133.             x = numstringOnes[int(numinput[-7])] + ' Million '
134.             a = x
135.         else:
136.             x = numstringTens[int(numinput[-8])]
137.             if int(numinput[-7]) > 0:
138.                 y = ' ' + numstring[int(numinput[-7])] + ' Million '
139.                 a = x + y
140.             else:
141.                 y = ' Million '
142.                 a = x + y
143.         return a + hundredthousands(numinput)
144.
145.     # for a 9 digit input
146.     def hundredmillion(numinput):
147.         x = numstring[int(numinput[-9])]
148.         if int(numinput[-8]) > 0:
149.             y = ' Hundred '
150.         else:
151.             y = ' Hundred'
152.         return x + y + tenmillion(numinput)
153.
154.
155.     # Determines which variation of the outputtable digits it will use
156.     if length == 1:
157.         print(f'The number is {(str(userInput))}\n{zero(userInput)}')
158.     elif length == 2:
159.         print(f'The number is {(str(userInput))}\n{tens(userInput)}')
160.     elif length == 3:
161.         print(f'The number is {(str(userInput))}\n{hundreds(userInput)}')
162.     elif length == 4:
163.         print(f'The number is {(str(userInput))[0]},{(str(userInput))[1:4]} \n{thousands(userInput)}')
164.     elif length == 5:
165.         print(f'The number is {(str(userInput))[0:2]},{(str(userInput))[2:5]} \n{tenthousands(userInput)}')
166.     elif length == 6:
167.         print(f'The number is {(str(userInput))[0:3]},{(str(userInput))[3:6]} \n{hundredthousands(userInput)}')
168.     elif length == 7:
169.         print(f'The number is {(str(userInput))[0]},{(str(userInput))[1:4]},{(str(userInput))[4:7]}\n'
170.               f'{million(userInput)}')
171.     elif length == 8:
172.         print(f'The number is {(str(userInput))[0:2]},{(str(userInput))[2:5]},{(str(userInput))[5:8]}\n'
173.               f'{tenmillion(userInput)}')
174.     elif length == 9:
175.         print(f'The number is {(str(userInput))[0:3]},{(str(userInput))[3:6]},{(str(userInput))[6:9]}\n'
176.               f'{hundredmillion(userInput)}')
177.     rerun = input('Run again? ')