Advertisement
cm12321

Spanish number spelling game

Nov 17th, 2019
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.74 KB | None | 0 0
  1. import random
  2.  
  3.  
  4. def number_to_word(n):
  5.     assert 0 < n < 10**9, "This only works for numbers between 1 and 999,999,999"
  6.  
  7.     irregulars = {0: "",
  8.                   1: "uno",
  9.                   2: "dos",
  10.                   3: "tres",
  11.                   4: "cuatro",
  12.                   5: "cinco",
  13.                   6: "seis",
  14.                   7: "siete",
  15.                   8: "ocho",
  16.                   9: "nueve",
  17.                   10: "diez",
  18.                   11: "once",
  19.                   12: "doce",
  20.                   13: "trece",
  21.                   14: "catorce",
  22.                   15: "quince",
  23.                   16: "dieciséis",
  24.                   17: "diecisiete",
  25.                   18: "dieciocho",
  26.                   19: "diecinueve",
  27.                   20: "veinte",
  28.                   21: "veintiuno",
  29.                   22: "veintidós",
  30.                   23: "veintitrés",
  31.                   24: "veinticuatro",
  32.                   25: "veinticinco",
  33.                   26: "veintiséis",
  34.                   27: "veintisiete",
  35.                   28: "veintiocho",
  36.                   29: "veintinueve",
  37.                   30: "treinta",
  38.                   40: "cuarenta",
  39.                   50: "cincuenta",
  40.                   60: "sesenta",
  41.                   70: "setenta",
  42.                   80: "ochenta",
  43.                   90: "noventa",
  44.                   100: "cien",
  45.                   200: "doscientos",
  46.                   300: "trescientos",
  47.                   400: "cuatrocientos",
  48.                   500: "quinientos",
  49.                   600: "seiscientos",
  50.                   700: "setecientos",
  51.                   800: "ochocientos",
  52.                   900: "novecientos",
  53.                   1000: "mil",
  54.                   1000000: "un millión"
  55.                   }
  56.  
  57.     if n in irregulars:
  58.         return irregulars[n]
  59.  
  60.     final_string = ""
  61.  
  62.     # get a lsit of the 3 digit values seperated by commas in n
  63.     three_digits = []
  64.     for i in range(len(str(n)) // 3 + 1):
  65.         if n % 1000 != 0 or str(n)[-3:] == "000":
  66.             three_digits.append(n % 1000)
  67.             n //= 1000
  68.  
  69.     def upto999(n):
  70.         if n in irregulars:
  71.             return irregulars[n]
  72.         elif 0 < n < 100:
  73.             return "{} y {}".format(irregulars[n - n % 10], irregulars[n % 10])
  74.         elif str(n)[0] == "1":
  75.             return "ciento {}".format(upto999(n % 100))
  76.         else:
  77.             return "{} {}".format(irregulars[n - n % 100], upto999(n % 100))
  78.  
  79.     if len(three_digits) >= 1:
  80.         final_string = str(upto999(three_digits[0]))
  81.  
  82.     if len(three_digits) >= 2:
  83.         if three_digits[1] == 0:
  84.             pass
  85.         elif three_digits[1] == 1:
  86.             final_string = "mil " + final_string
  87.         else:
  88.             final_string = str(
  89.                 upto999(three_digits[1])) + " mil " + final_string
  90.  
  91.     if len(three_digits) >= 3:
  92.         if three_digits[2] == 0:
  93.             pass
  94.         elif three_digits[2] == 1:
  95.             final_string = "un millión " + final_string
  96.         else:
  97.             final_string = str(
  98.                 upto999(three_digits[2])) + " milliones " + final_string
  99.  
  100.     return final_string
  101.  
  102.  
  103. def main():
  104.     streak = 0
  105.     print("\n\nNote: All numbers must be between 1 and 999,999,999 for this program to work.")
  106.     low = int(input(
  107.         "\nPlease input the lower bound of the numbers you want to be tested on: "))
  108.     high = int(input(
  109.         "\nPlease input the upper bound of the numbers you want to be tested on: "))
  110.  
  111.     play_again = True
  112.     while play_again:
  113.         test = random.randint(low, high)
  114.         user_guess = input(
  115.             "\nPlease try and spell the number {}: ".format(test))
  116.         if user_guess.lower() == number_to_word(test):
  117.             streak += 1
  118.             print("\nCorrect! Your current streak is {}".format(streak))
  119.         else:
  120.             streak = 0
  121.             print("\nSorry, that wasn't correct. The correct spelling was \"{}\"".format(
  122.                 number_to_word(test)))
  123.  
  124.         while True:
  125.             what_next = input(
  126.                 "\nWhat do you want to do now? (P)lay again, (Q)uit, or (C)hange numbers? ").upper()
  127.             if what_next == "P":
  128.                 break
  129.             elif what_next == "Q":
  130.                 play_again = False
  131.                 break
  132.             elif what_next == "C":
  133.                 low = int(input(
  134.                     "\nPlease input the lower bound of the numbers you want to be tested on: "))
  135.                 high = int(input(
  136.                     "\nPlease input the upper bound of the numbers you want to be tested on: "))
  137.                 break
  138.             else:
  139.                 print("\nSorry, that wasn't a recognised command")
  140.  
  141.  
  142. if __name__ == "__main__":
  143.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement