SHARE
TWEET

Untitled

a guest Oct 12th, 2017 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Add to the program intName.py in ch 5, section 5 of the book.
  2. # This program turns an integer into its English name.
  3. #Program is working 100% and fufills all requirments!
  4.  
  5. def intName(number) :
  6.    ''' Turn a number into its English name
  7.        @param: number (int)
  8.        @return: the name (str)
  9.    '''  
  10.    part = number   # The part that still needs to be converted.
  11.    name = ""   # The name of the number.
  12.    
  13.    
  14.    if part >= 20000 :#This part is working...... for all numbers 20,000 - 999,999!!!
  15.       name += secondFunc(part // 1000) + " thousand " #seperates the numbers
  16.       part = part % 1000  #reduces the number/part to be out of the thousands place
  17.       #print("Test 1", part)  
  18.    
  19.    
  20.    if part >= 10000 :#THis part is working...... from 10,000-19,999
  21.       name += teenName(part // 1000) + " thousand "#we could technically get rid of this...
  22.       part = part % 1000  
  23.       #print("Test 1", part)
  24.    
  25.    if part >= 1000 :#works up to 9,999
  26.       name += digitName(part // 1000) + " thousand "
  27.       part = part % 1000  
  28.       #print("Test 2", part)
  29.    
  30.    if part >= 100 :
  31.       name += digitName(part // 100) + " hundered"
  32.       part = part % 100
  33.        
  34.  
  35.    if part >= 20 :
  36.       name = name + " " + tensName(part) #Changed
  37.       #print("Test 3", part)
  38.      
  39.       if part > 20:
  40.          name = name + "-" #adds the "- between
  41.          
  42.       part = part % 10
  43.    elif part >= 10 :
  44.       name = name + " " + teenName(part)
  45.       part = 0
  46.       #print("Test 4", part)
  47.      
  48.  
  49.  
  50.    if part > 0 :
  51.       name = name + "" + digitName(part)
  52.       #print(part)
  53.      
  54.    
  55.    
  56.      
  57.    name = name.strip()#removes all trailing and leading whitespace
  58.    return name
  59.    #PART 3 & 4 goes in here too!
  60.  
  61.  
  62. def secondFunc(number):#TEST FUNC... works....
  63.    '''
  64.    takes in a number from the intName Function.
  65.    returns the name.  This aprt is sues for the thousands place
  66.    (Only called for numbers greator than 20,000
  67.    '''
  68.    part = number   # The part that still needs to be converted.
  69.    name = ""   # The name of the number.    
  70.    
  71.    if part >= 100 :
  72.       name += digitName(part // 100) + " hundered"
  73.       part = part % 100
  74.        
  75.  
  76.    if part >= 20 :
  77.       name = name + " " + tensName(part) #Changed
  78.       #print("Test 3", part)
  79.      
  80.       if part > 20:
  81.          name = name + "-" #adds the "- between
  82.          
  83.       part = part % 10
  84.    elif part >= 10 :
  85.       name = name + " " + teenName(part)
  86.       part = 0
  87.       #print("Test 4", part)
  88.      
  89.  
  90.  
  91.    if part > 0 :
  92.       name = name + "" + digitName(part)
  93.       #print(part)
  94.      
  95.    name = name.strip()#removes all trailing and leading whitespace
  96.    return name
  97.    #PART 3 goes in here too!
  98.    
  99. def digitName(digit) :
  100.    ''' Turn a digit into its English name
  101.        @param: digit (int)
  102.        @return: name (str)
  103.    '''  
  104.    if digit == 1 : return "one"
  105.    if digit == 2 : return "two"
  106.    if digit == 3 : return "three"
  107.    if digit == 4 : return "four"
  108.    if digit == 5 : return "five"
  109.    if digit == 6 : return "six"
  110.    if digit == 7 : return "seven"
  111.    if digit == 8 : return "eight"
  112.    if digit == 9 : return "nine"
  113.    return ""
  114.  
  115.  
  116. def teenName(number) :
  117.    ''' Turn a number between 10 and 19 into its English name
  118.        @param: number (int)
  119.        @return: name (str)
  120.    '''  
  121.    if number == 10 : return "ten"
  122.    if number == 11 : return "eleven"
  123.    if number == 12 : return "twelve"
  124.    if number == 13 : return "thirteen"
  125.    if number == 14 : return "fourteen"
  126.    if number == 15 : return "fifteen"
  127.    if number == 16 : return "sixteen"
  128.    if number == 17 : return "seventeen"
  129.    if number == 18 : return "eighteen"
  130.    if number == 19 : return "nineteen"
  131.    return ""
  132.  
  133.  
  134. def tensName(number) :
  135.    ''' Turn a number between 20 and 99 into its English name
  136.        @param: number (int)
  137.        @return: name (str)
  138.    '''  
  139.    if number >= 90 : return "ninety"
  140.    if number >= 80 : return "eighty"
  141.    if number >= 70 : return "seventy"
  142.    if number >= 60 : return "sixty"
  143.    if number >= 50 : return "fifty"
  144.    if number >= 40 : return "forty"
  145.    if number >= 30 : return "thirty"
  146.    if number >= 20 : return "twenty"
  147.    return ""
  148.  
  149.  
  150. def main() :
  151.    value = ""
  152.      
  153.    while True:#Part 5
  154.       try:#exception handeling for simplicty sake....
  155.          value = int(input("Please enter a positive integer < 1,000,000 (or 0 to end): "))
  156.          if(value == 0):
  157.             print("End of program. Thanks!")
  158.             break
  159.          print(intName(value))            
  160.       except ValueError:
  161.          print("Sorry, an invalid inout was detected please try again")
  162.          continue
  163.    
  164.    
  165. # Start the program.
  166. main()
RAW Paste Data
Top