Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Katakana quiz
#!/usr/bin/env python3
# coding=utf-8
import random
vowels = ["a", "i", "u", "e", "o"]
predicate_consonants = ["k", "g", "s", "z", "t", "d", "n", "h", "b", "p"]
syllables = []
_chars = "アイウエオカキクケコガギグゲゴサシスセソザジズゼゾタチツテトダヂヅデドナニヌネノハヒフヘホバビブベボパピプペポ"
_map = {}
def make_syllables():
syllables = vowels.copy()
for c in predicate_consonants:
for v in vowels:
syl = "{}{}".format(c, v)
if syl == "si" : syl = "shi"
if syl == "zi" : syl = "ji"
if syl == "ti" : syl = "chi"
if syl == "tu" : syl = "tsu"
syllables.append(syl)
return syllables
def make_map(syllables, _chars):
return dict(zip(syllables, _chars))
def make_random_word(syllables, _map):
wordlength = random.randint(2, 5)
word = {"en": "", "ja": ""}
for i in range(wordlength):
syllable = random.choice(syllables)
word["ja"] = word["ja"] + _map[syllable]
word["en"] = word["en"] + syllable
return word
def main():
syllables = make_syllables()
_map = make_map(syllables, _chars)
print("Katakana quiz")
_user_input = ""
while _user_input.strip() == "":
word = make_random_word(syllables, _map)
print("Write this (made-up) word: {}".format(word["en"]))
_user_input = input()
print(" Answer: {}".format(word["ja"]))
print("")
if __name__ == "__main__": main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.