Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- # Variables for the words
- numstring = ["", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"]
- # Variation of the first numstring that is used for one-digit numbers
- numstringZero = ["Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"]
- numstringTens = ['', '', 'Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety']
- numstringOnes = ['Ten', 'Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen',
- 'Nineteen']
- # Used to repeat the program until the user says to end. Uses a while loop
- rerun = 'Y'
- while rerun.upper() == 'Y':
- print('This program can convert numbers from 1-999,999,999 to TEXT. \n'
- 'The input is of type integer, you must not enter commas.')
- # Input for the User
- option = input('Would you like the program generate a random number? Y/N: ')
- if option.upper() == 'Y':
- userInput = str(random.randint(0, 999999999))
- else:
- userInput = input("Enter a number: ")
- # Gets the amount of numbers
- length = len(userInput)
- # Outputs the amount of digits
- print(f'It has {length} digits.')
- # message when the inputted number is too long
- if length > 9:
- print('Number is too long convert to words, please keep it to 9 digits only.')
- # for a 1 digit input
- def zero(numinput):
- x = numstringZero[int(numinput[-1])]
- return x
- # for a 1 digit input
- def ones(numinput):
- x = numstring[int(numinput[-1])]
- return x
- # for a 2 digit input
- def tens(numinput):
- if int(numinput[-2]) == 1:
- a = numstringOnes[int(numinput[-1])]
- elif int(numinput[-2]) == 0:
- a = numstring[int(numinput[-1])]
- else:
- a = numstringTens[int(numinput[-2])] + ' ' + ones(numinput)
- return a
- # for a 3 digit input
- def hundreds(numinput):
- b = numstring[int(numinput[-3])]
- if int(numinput[-3]) > 0:
- a = b + ' Hundred '
- else:
- a = b + ''
- return a + tens(numinput)
- # for a 4 digit input
- def thousands(numinput):
- x = numstring[int(numinput[-4])]
- if int(numinput[-4]) > 0:
- a = x + ' Thousand '
- else:
- if int(numinput[-5]) > 0:
- if int(numinput[-4]) > 0:
- a = x + 'Thousand '
- else:
- a = x + ' Thousand '
- else:
- if int(numinput[-0]) > 0:
- if int(numinput[-6]) > 0:
- a = x + 'Thousand '
- else:
- a = x + ''
- else:
- a = x + ''
- return a + hundreds(numinput)
- # variation of the 4 digit input that takes into account inputted zeroes
- def thousandszero(numinput):
- if int(numinput[-4]) > 0:
- x = ' Thousand '
- else:
- if int(numinput[-5]) > 0:
- if int(numinput[-5]) == 1:
- x = ' Thousand '
- else:
- x = 'Thousand '
- else:
- x = ''
- return x + hundreds(numinput)
- # for a 5 digit input
- def tenthousands(numinput):
- if int(numinput[-5]) == 1:
- a = numstringOnes[int(numinput[-4])] + thousandszero(numinput)
- else:
- x = numstringTens[int(numinput[-5])]
- if int(numinput[-4]) == 0:
- y = ''
- else:
- y = ' '
- a = x + y + thousands(numinput)
- return a
- # for a 6 digit input
- def hundredthousands(numinput):
- x = numstring[int(numinput[-6])]
- if int(numinput[-6]) > 0:
- if int(numinput[-5]) > 0:
- y = ' Hundred '
- else:
- if int(numinput[-4]) > 0:
- y = ' Hundred'
- else:
- y = ' Hundred '
- a = x + y + tenthousands(numinput)
- else:
- a = x + tenthousands(numinput)
- return a
- # for a 7 digit input
- def million(numinput):
- x = numstring[int(numinput[-7])]
- y = ' Million '
- return x + y + hundredthousands(numinput)
- # for 8 digit input
- def tenmillion(numinput):
- if int(numinput[-8]) == 1:
- x = numstringOnes[int(numinput[-7])] + ' Million '
- a = x
- else:
- x = numstringTens[int(numinput[-8])]
- if int(numinput[-7]) > 0:
- y = ' ' + numstring[int(numinput[-7])] + ' Million '
- a = x + y
- else:
- y = ' Million '
- a = x + y
- return a + hundredthousands(numinput)
- # for a 9 digit input
- def hundredmillion(numinput):
- x = numstring[int(numinput[-9])]
- if int(numinput[-8]) > 0:
- y = ' Hundred '
- else:
- y = ' Hundred'
- return x + y + tenmillion(numinput)
- # Determines which variation of the outputtable digits it will use
- if length == 1:
- print(f'The number is {(str(userInput))}\n{zero(userInput)}')
- elif length == 2:
- print(f'The number is {(str(userInput))}\n{tens(userInput)}')
- elif length == 3:
- print(f'The number is {(str(userInput))}\n{hundreds(userInput)}')
- elif length == 4:
- print(f'The number is {(str(userInput))[0]},{(str(userInput))[1:4]} \n{thousands(userInput)}')
- elif length == 5:
- print(f'The number is {(str(userInput))[0:2]},{(str(userInput))[2:5]} \n{tenthousands(userInput)}')
- elif length == 6:
- print(f'The number is {(str(userInput))[0:3]},{(str(userInput))[3:6]} \n{hundredthousands(userInput)}')
- elif length == 7:
- print(f'The number is {(str(userInput))[0]},{(str(userInput))[1:4]},{(str(userInput))[4:7]}\n'
- f'{million(userInput)}')
- elif length == 8:
- print(f'The number is {(str(userInput))[0:2]},{(str(userInput))[2:5]},{(str(userInput))[5:8]}\n'
- f'{tenmillion(userInput)}')
- elif length == 9:
- print(f'The number is {(str(userInput))[0:3]},{(str(userInput))[3:6]},{(str(userInput))[6:9]}\n'
- f'{hundredmillion(userInput)}')
- rerun = input('Run again? ')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement