Last active
August 29, 2016 21:52
-
-
Save NoraCodes/ecb9dcbe44091b9f077d0cb4e0147b0a to your computer and use it in GitHub Desktop.
Numerica - Practice Japanese numbers with the help of a computer!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
This is a program to help you study the Japanese numbers. | |
It currently goes from 0 to 99; I will extend it at a later date. | |
It can be executed as follows: | |
./numerica.py | |
which will do all the available numbers, or | |
./numberica.py 10 | |
which will go only up to 10. | |
""" | |
numbers = ['ZERO', | |
'いち', | |
'に', | |
'さん', | |
'よん', | |
'ご', | |
'ろく', | |
'なな', | |
'はち', | |
'きゅう', | |
'じゅう', | |
] | |
class OutOfRangeException(Exception): | |
pass | |
def small_to_japanese(n): | |
"Convert a number (0-10) to Japanese." | |
if n > 10 or n < 0: | |
raise OutOfRangeException | |
return numbers[n] | |
def medium_to_japanese(n): | |
"Convert a number from 11 - 100 to Japanese" | |
if n > 100 or n < 11: | |
raise OutOfRangeException | |
digits = list(map( | |
int, str(n) | |
)) | |
out = "" | |
# Omit いち in numbers > 10 | |
if digits[0] > 1: | |
out += numbers[digits[0]] + " " | |
out += numbers[10] + " " | |
out += numbers[digits[1]] | |
return out | |
def number_to_japanese(n): | |
try: | |
return small_to_japanese(n) | |
except OutOfRangeException: | |
pass | |
try: | |
return medium_to_japanese(n) | |
except OutOfRangeException: | |
pass | |
print("No way to represent numbers of that magnitude!") | |
if __name__ == "__main__": | |
from random import randint | |
from sys import argv | |
# Check if there is a command line option for max numbers | |
if len(argv) >= 2: | |
try: | |
MAX_NUM = int(argv[1]) | |
except ValueError: | |
MAX_NUM = -1 | |
# A little edge case handling | |
if MAX_NUM > 99: | |
print("Impossible - this program doesn't " | |
"work with numbers over 99.") | |
exit(1) | |
else: | |
# If a max wasn't given, default to 99 | |
MAX_NUM = 99 | |
given = "" | |
done_so_far = [] | |
number_done = 0 | |
while True: | |
n = randint(0, MAX_NUM) | |
# If and as long as n has already been done, get a new number. | |
while n in done_so_far: | |
n = randint(0, MAX_NUM) | |
try: | |
given = input("What is {} in Roman numbers? ".format( | |
number_to_japanese(n))) | |
except KeyboardInterrupt: | |
print("Bye!") | |
exit(1) | |
except EOFError: | |
print("Bye!") | |
exit(1) | |
if given.lower() == 'quit': | |
print("Bye!") | |
exit(0) | |
if number_done >= MAX_NUM: | |
print("You did all the numbers in that set!") | |
exit(0) | |
try: | |
given_n = int(given) | |
except ValueError: | |
given_n = -1 | |
if given_n == n: | |
print("You got it!") | |
done_so_far.append(n) | |
number_done += 1 | |
else: | |
print("No, that's incorrect. This is {}.".format(n)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment