Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- def number_to_word(n):
- assert 0 < n < 10**9, "This only works for numbers between 1 and 999,999,999"
- irregulars = {0: "",
- 1: "uno",
- 2: "dos",
- 3: "tres",
- 4: "cuatro",
- 5: "cinco",
- 6: "seis",
- 7: "siete",
- 8: "ocho",
- 9: "nueve",
- 10: "diez",
- 11: "once",
- 12: "doce",
- 13: "trece",
- 14: "catorce",
- 15: "quince",
- 16: "dieciséis",
- 17: "diecisiete",
- 18: "dieciocho",
- 19: "diecinueve",
- 20: "veinte",
- 21: "veintiuno",
- 22: "veintidós",
- 23: "veintitrés",
- 24: "veinticuatro",
- 25: "veinticinco",
- 26: "veintiséis",
- 27: "veintisiete",
- 28: "veintiocho",
- 29: "veintinueve",
- 30: "treinta",
- 40: "cuarenta",
- 50: "cincuenta",
- 60: "sesenta",
- 70: "setenta",
- 80: "ochenta",
- 90: "noventa",
- 100: "cien",
- 200: "doscientos",
- 300: "trescientos",
- 400: "cuatrocientos",
- 500: "quinientos",
- 600: "seiscientos",
- 700: "setecientos",
- 800: "ochocientos",
- 900: "novecientos",
- 1000: "mil",
- 1000000: "un millión"
- }
- if n in irregulars:
- return irregulars[n]
- final_string = ""
- # get a lsit of the 3 digit values seperated by commas in n
- three_digits = []
- for i in range(len(str(n)) // 3 + 1):
- if n % 1000 != 0 or str(n)[-3:] == "000":
- three_digits.append(n % 1000)
- n //= 1000
- def upto999(n):
- if n in irregulars:
- return irregulars[n]
- elif 0 < n < 100:
- return "{} y {}".format(irregulars[n - n % 10], irregulars[n % 10])
- elif str(n)[0] == "1":
- return "ciento {}".format(upto999(n % 100))
- else:
- return "{} {}".format(irregulars[n - n % 100], upto999(n % 100))
- if len(three_digits) >= 1:
- final_string = str(upto999(three_digits[0]))
- if len(three_digits) >= 2:
- if three_digits[1] == 0:
- pass
- elif three_digits[1] == 1:
- final_string = "mil " + final_string
- else:
- final_string = str(
- upto999(three_digits[1])) + " mil " + final_string
- if len(three_digits) >= 3:
- if three_digits[2] == 0:
- pass
- elif three_digits[2] == 1:
- final_string = "un millión " + final_string
- else:
- final_string = str(
- upto999(three_digits[2])) + " milliones " + final_string
- return final_string
- def main():
- streak = 0
- print("\n\nNote: All numbers must be between 1 and 999,999,999 for this program to work.")
- low = int(input(
- "\nPlease input the lower bound of the numbers you want to be tested on: "))
- high = int(input(
- "\nPlease input the upper bound of the numbers you want to be tested on: "))
- play_again = True
- while play_again:
- test = random.randint(low, high)
- user_guess = input(
- "\nPlease try and spell the number {}: ".format(test))
- if user_guess.lower() == number_to_word(test):
- streak += 1
- print("\nCorrect! Your current streak is {}".format(streak))
- else:
- streak = 0
- print("\nSorry, that wasn't correct. The correct spelling was \"{}\"".format(
- number_to_word(test)))
- while True:
- what_next = input(
- "\nWhat do you want to do now? (P)lay again, (Q)uit, or (C)hange numbers? ").upper()
- if what_next == "P":
- break
- elif what_next == "Q":
- play_again = False
- break
- elif what_next == "C":
- low = int(input(
- "\nPlease input the lower bound of the numbers you want to be tested on: "))
- high = int(input(
- "\nPlease input the upper bound of the numbers you want to be tested on: "))
- break
- else:
- print("\nSorry, that wasn't a recognised command")
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement