# In need of help with Python programming

asked May 14, 2020
Write a program that turns the given number to letters (like one thousand six hundred and ninety nine). The number has to be between  1 and 99999999999.

answered May 19, 2020 by (190 points)
edited May 20, 2020 by Tanner Titlow
I read the question wrong and did the opposite first hahaha both were fun to do though. Cheers!

ONES = [1, 2, 3, 4, 5, 6, 7, 8, 9]
TENS = [20, 30, 40, 50, 60, 70, 80, 90]
BENS = [100, 200, 300, 400, 500, 600, 700, 800, 900]
RACKS = [1000, 1000000, 1000000000]

def main():
again = 'y'
while again.lower() == 'y':
num_string = input("Enter a number in word format: ")

print("In number form, that is ", format(change_to_num(num_string), ',d'), '.', sep='')

again = input("Do you have another number? [y/n]: ")

def change_to_num(num_string):
num_list = get_num_list(num_string)
if len(num_list) == 1:
tot = num_list[0]
else:
tot = 0
num_list = get_bens(num_list)
num_list = mult_racks(num_list)
for i in num_list:
tot += i

def get_num_list(num_string):
str_list = num_string.split(' ')
num_list = []
for item in str_list:
if item != 'and':
num_list.append(get_num(item))
return num_list
def get_num(string):
if string.lower() == 'one':
num = 1
elif string.lower() == 'two':
num = 2
elif string.lower() == 'three':
num = 3
elif string.lower() == 'four':
num = 4
elif string.lower() == 'five':
num = 5
elif string.lower() == 'six':
num = 6
elif string.lower() == 'seven':
num = 7
elif string.lower() == 'eight':
num = 8
elif string.lower() == 'nine':
num = 9
elif string.lower() == 'ten':
num = 10
elif string.lower() == 'eleven':
num = 11
elif string.lower() == 'twelve':
num = 12
elif string.lower() == 'thirteen':
num = 13
elif string.lower() == 'fourteen':
num = 14
elif string.lower() == 'fifteen':
num = 15
elif string.lower() == 'sixteen':
num = 16
elif string.lower() == 'seventeen':
num = 17
elif string.lower() == 'eighteen':
num = 18
elif string.lower() == 'nineteen':
num = 19
elif string.lower() == 'twenty':
num = 20
elif string.lower() == 'thirty':
num = 30
elif string.lower() == 'forty':
num = 40
elif string.lower() == 'fifty':
num = 50
elif string.lower() == 'sixty':
num = 60
elif string.lower() == 'seventy':
num = 70
elif string.lower() == 'eighty':
num = 80
elif string.lower() == 'ninety':
num = 90
elif string.lower() == 'hundred':
num = 100
elif string.lower() == 'thousand':
num = 1000
elif string.lower() == 'million':
num = 1000000
elif string.lower() == 'billion':
num = 1000000000

return num

index = 0
del_list = []
temp_list = []
for el in num_list:
if index != len(num_list)-1:
if el in TENS and num_list[index+1] in ONES:
del_list.append(index+1)
added = el + num_list[index+1]
index += 1
else:
temp_list.append(el)
index += 1

else:
temp_list.append(el)
for i in del_list:
temp_list[i] = 0

for c in range(len(del_list)):
temp_list.remove(0)

return temp_list

def get_bens(num_list):
temp_list = []
del_list = []
index = 0
for el in num_list:
if index != 0:
if el == 100:
del_list.append(index-1)
ben = num_list[index-1] * el
temp_list.append(ben)
else:
temp_list.append(el)
else:
temp_list.append(el)
index += 1

for i in del_list:
temp_list[i] = 0

for c in range(len(del_list)):
temp_list.remove(0)

return temp_list

index = 0
del_list = []
temp_list = []
for el in num_list:
if index != len(num_list)-1:
if el in BENS and num_list[index+1] not in RACKS:
del_list.append(index+1)
added = el + num_list[index+1]
else:
temp_list.append(el)
index += 1
else:
temp_list.append(el)

for i in del_list:
temp_list[i] = 0

for c in range(len(del_list)):
temp_list.remove(0)

return temp_list

def mult_racks(num_list):
temp_list = []
index = 0
for el in num_list:
if index != 0:
if el in RACKS:
combine = num_list[index-1] * el
temp_list.append(combine)
del temp_list[temp_list.index(combine)-1]
else:
temp_list.append(el)
else:
temp_list.append(el)
index += 1

return temp_list

main()
answered May 19, 2020 by (190 points)
edited May 20, 2020 by Tanner Titlow

I didn't take the time to comment this out so if you want the logic explained just shoot me a reply. It could probably be done in an easier way, but I'm only one semester into my CS degree. This was a fun problem!

def main():
again = 'y'
while again.lower() == 'y':
number = int(input("Enter a number: "))
print("In word format, that's", change_to_string(number) + '.')
again = input("Do you have another number? [y/n]: ")

def change_to_string(number):
place_list = format(number, ',d').split(',')

string_list = []

for place in place_list:
if len(place) == 3:
string_list.append(str_ben(place))

elif len(place) == 2:
string_list.append(str_ten(place))

elif len(place) == 1:
string_list.append(str_one(place))

num_string = get_num_string(string_list)

return num_string

def str_ben(number):
str_num = ''

if number[0] == '0':
str_num = ''
elif number[1:] == '00':
str_num += str_one(number[0]) + ' hundred'

else:
str_num += str_one(number[0]) + ' hundred and ' + str_ten(number[1:])

return str_num

def str_ten(number):
str_num = ''
tens = int(number[0])
ones = int(number[1])

if tens == 0:
str_num += str_one(ones)

elif tens == 1:
if ones == 0:
str_num += 'ten'
elif ones == 1:
str_num += 'eleven'
elif ones == 2:
str_num += 'twelve'
elif ones == 3:
str_num += 'thirteen'
elif ones == 4:
str_num += 'fourteen'
elif ones == 5:
str_num += 'fifteen'
elif ones == 6:
str_num += 'sixteen'
elif ones == 7:
str_num += 'seventeen'
elif ones == 8:
str_num += 'eighteen'
elif ones == 9:
str_num += 'nineteen'
elif tens == 2:
if ones == 0:
str_num += 'twenty'
else:
str_num += 'twenty ' + str_one(ones)
elif tens == 3:
if ones == 0:
str_num += 'thirty'
else:
str_num += 'thirty ' + str_one(ones)
elif tens == 4:
if ones == 0:
str_num += 'forty'
else:
str_num += 'forty ' + str_one(ones)
elif tens == 5:
if ones == 0:
str_num += 'fifty'
else:
str_num += 'fifty ' + str_one(ones)
elif tens == 6:
if ones == 0:
str_num += 'sixty'
else:
str_num += 'sixty ' + str_one(ones)
elif tens == 7:
if ones == 0:
str_num += 'seventy'
else:
str_num += 'seventy ' + str_one(ones)
elif tens == 8:
if ones == 0:
str_num += 'eighty'
else:
str_num += 'eighty ' + str_one(ones)
elif tens == 9:
if ones == 0:
str_num += 'ninety'
else:
str_num += 'ninety ' + str_one(ones)

return str_num

def str_one(number):
num = int(number)

if num == 0:
string = ''
elif num == 1:
string = 'one'
elif num == 2:
string = 'two'
elif num == 3:
string = 'three'
elif num == 4:
string = 'four'
elif num == 5:
string = 'five'
elif num == 6:
string = 'six'
elif num == 7:
string = 'seven'
elif num == 8:
string = 'eight'
elif num == 9:
string = 'nine'

return string

def get_num_string(string_list):
string = ''

place = len(string_list)

if place == 1:
string += string_list[0]
else:
for value in string_list:
string += value
if place == 4 and value != '':
string += ' billion '
elif place == 3 and value != '':
string += ' million '
elif place == 2 and value != '':
string += ' thousand '
place -= 1

return string.rstrip()
main()