Facebook
From ohmypog, 1 Month ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 34
  1. dict_ones = {
  2.         0: "",
  3.         1: "one",
  4.         2: "two",
  5.         3: "three",
  6.         4: "four",
  7.         5: "five",
  8.         6: "six",
  9.         7: "seven",
  10.         8: "eight",
  11.         9: "nine",
  12. }
  13.  
  14. dict_tens = {
  15.         2: "twenty",
  16.         3: "thirty",
  17.         4: "forty",
  18.         5: "fifty",
  19.         6: "sixty",
  20.         7: "seventy",
  21.         8: "eighty",
  22.         9: "ninety",
  23. }
  24.  
  25. dict_exception = {
  26.         10: "ten",
  27.         11: "eleven",
  28.         12: "twelve",
  29.         13: "thirteen",
  30.         14: "fourteen",
  31.         15: "fifteen",
  32.         16: "sixteen",
  33.         17: "seventeen",
  34.         18: "eighteen",
  35.         19: "nineteen",
  36. }
  37.  
  38. def num_to_string(num):
  39.     if(num == 1000):
  40.         return "one thousand"
  41.        
  42.     if(num>=0 and num<10):
  43.         return dict_ones.get(num)
  44.  
  45.     if(num>=10 and num<=19):
  46.         return dict_exception.get(num)
  47.        
  48.     if(num>=20 and num<=99):
  49.         return dict_tens.get(int(num/10)) + " " + dict_ones.get(num%10)
  50.  
  51.     if(num>=100 and num<=999):
  52.         return dict_ones.get(int(num/100)) + " hundred " + num_to_string(num%100)
  53.  
  54. if __name__ == '__main__':
  55.         for i in range(1, 1001):
  56.                 print(f"{i}: {num_to_string(i)}")