Results 1 to 4 of 4

Thread: help make code shorter?

  1. #1
    Join Date
    Sep 2012
    Beans
    91

    help make code shorter?

    I'm trying to make a simple cli app to help me learn my kanji but I they way I'm doing it right now is just to long and boring and I can't come up with any code that will work to make it shorter.

    Code:
    kanji_set1 = ['九', '古', '冒', '朋', '明', '唱', '晶', '品', '呂', '昌', '早', '旭',\
                '世', '胃', '旦', '胆', '亘', '凹', '凸']
    
    #kanji set one kanji reading only here so I can remember the readings for each kanji
    #also failed attempt to make code shorter
    kanji1_onread = ['きゅう','ふるい', 'ぼう', 'ほう', 'めい', 'しょう', 'しょう',\
                    'しな', 'りょ', 'しょう', 'そう', 'きょく', 'せい', 'い', 'たん', 'たん',\
                    'こう', 'おう', 'とつ']
    
    # kanji set one english meaning
    kanji1_e = ['nine', 'old', 'risk', 'companion', 'bright', 'chant', 'sparkle',\
                'goods', 'spine/ come together', 'prosperous','early' , 'rising sun',\
                'generation', 'stomach', 'nightbreak', 'gall bladder', 'span', \
                'concave', 'convex']
    
    def check_exit(input):
        if input.lower() == 'quit' or  input.lower() == 'exit':
            sys.exit()
        
    print "どうしてる? This is an exercise based on RTF lessons 1 though 2 to get\
        to guess the meaning of the kanji and the on reading."
    print "First guess the meaning of the kanji in english if you get it right\
        you'll be able to guess it's on reading."
    print "Type any on reading in either hiragana or romanji. Type quit or exit to quit."
    
    
    while True:
        kanji = random.choice(kanji_set1)
        print kanji
        guess = raw_input('>>')
        check_exit(guess)
        
        if kanji == '九':
            if guess == 'nine':
                print "Correct!"
                on_guess = raw_input('>')
                if on_guess == 'きゅう' or on_guess == 'kyuu':
                    print "correct!"
                else:
                    print "incorrect!"
            else:
                print "ちがう!that's wrong"
        
        elif kanji == '古':
            if guess == 'old':
                print "correct!"
                on_guess = raw_input('>')
                if on_guess == 'ふる' or on_guess == 'furu':
                    print "Correct!"
                else:
                    print "Incorrect!"
            else:
                print "Incorrect"
    also Is there any way I can make the kanji font size any bigger?

    P.S is there any way I can make kanji1_e a dict and make the value of a kanji in kanji_set1 equal to the key in kanji1_e and make it print the value of the key.
    Last edited by snowlizard31; October 20th, 2012 at 01:36 AM.

  2. #2
    Join Date
    Dec 2009
    Location
    Muscatine, IA
    Beans
    83
    Distro
    Lubuntu 13.10 Saucy Salamander

    Re: help make code shorter?

    Quote Originally Posted by snowlizard31 View Post
    I'm trying to make a simple cli app to help me learn my kanji but I they way I'm doing it right now is just to long and boring and I can't come up with any code that will work to make it shorter.

    Code:
    kanji_set1 = ['九', '古', '冒', '朋', '明', '唱', '晶', '品', '呂', '昌', '早', '旭',\
                '世', '胃', '旦', '胆', '亘', '凹', '凸']
    
    #kanji set one kanji reading only here so I can remember the readings for each kanji
    #also failed attempt to make code shorter
    kanji1_onread = ['きゅう','ふるい', 'ぼう', 'ほう', 'めい', 'しょう', 'しょう',\
                    'しな', 'りょ', 'しょう', 'そう', 'きょく', 'せい', 'い', 'たん', 'たん',\
                    'こう', 'おう', 'とつ']
    
    # kanji set one english meaning
    kanji1_e = ['nine', 'old', 'risk', 'companion', 'bright', 'chant', 'sparkle',\
                'goods', 'spine/ come together', 'prosperous','early' , 'rising sun',\
                'generation', 'stomach', 'nightbreak', 'gall bladder', 'span', \
                'concave', 'convex']
    
    def check_exit(input):
        if input.lower() == 'quit' or  input.lower() == 'exit':
            sys.exit()
        
    print "どうしてる? This is an exercise based on RTF lessons 1 though 2 to get\
        to guess the meaning of the kanji and the on reading."
    print "First guess the meaning of the kanji in english if you get it right\
        you'll be able to guess it's on reading."
    print "Type any on reading in either hiragana or romanji. Type quit or exit to quit."
    
    
    while True:
        kanji = random.choice(kanji_set1)
        print kanji
        guess = raw_input('>>')
        check_exit(guess)
        
        if kanji == '九':
            if guess == 'nine':
                print "Correct!"
                on_guess = raw_input('>')
                if on_guess == 'きゅう' or on_guess == 'kyuu':
                    print "correct!"
                else:
                    print "incorrect!"
            else:
                print "ちがう!that's wrong"
        
        elif kanji == '古':
            if guess == 'old':
                print "correct!"
                on_guess = raw_input('>')
                if on_guess == 'ふる' or on_guess == 'furu':
                    print "Correct!"
                else:
                    print "Incorrect!"
            else:
                print "Incorrect"
    also Is there any way I can make the kanji font size any bigger?

    P.S is there any way I can make kanji1_e a dict and make the value of a kanji in kanji_set1 equal to the key in kanji1_e and make it print the value of the key.
    It appears that you're using an array for the kanji, and english translations. So, you should be able to do something like this:

    Code:
    index = random(10)
    print kanji_set1(index)
    guess = raw_input('>>')
    while guess != "exit" OR guess !="quit":
        if guess == kanji_e(index):
            print "Correct! Now, guess the on reading of the kanji"
            on_guess = raw_input('>')
            if on_guess == kanji1_onread(index):
                    print "correct!"
            else:
                    print "incorrect!"
        else:
            print "Incorrect."
        index = random(10)
        print kanji_set1(index)
        guess = raw_input('>>')
    Some things to note. The actual format for each statement will vary based on whatever language you're using. Also the number in the random statement will vary depending on the language and how many items you have in the array.

    You won't need the check_exit() portion of your code at all, since it will be checked in the while loop.

    Also, I left off the top portion of the code. So you'll need to create each array like you did, then print the instructions, and start with the code that I put in.

    As for your PS, that's exactly what you should be doing with the arrays. You won't need to compare each element in the array separately. You'll just use one line to get the index, one line to print the kanji from that array at that index location, and one line to compare the guess to each corresponding array at that index location.

    Also, in case you're not familiar with this, the first position in the array is 0 NOT 1. So, if you have 10 items, you'll be looking for 0 through 9 (not 1 through 10). You'll have to adjust your random for that.

    Hope this helps. Have a great day
    Patrick.

  3. #3
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: help make code shorter?

    separate arrays are a pain in terms of maintenance.

    i'd create a file where all related values are together, read the file into array of dict structures and then pick one
    Code:
    #!/usr/bin/env python
    
    import codecs
    import random
    
    class KanjiDictElem():
      def __init__( self, kanji, read, english ):
        self.kanji = kanji
        self.read = read
        self.english = english
    
    f = codecs.open( "kanji_dict.txt", "r", encoding="utf-8" )
    kanjidict = []
    
    # populate dict
    for ln in f:
      col = ln.split( "|" )
      kanjidict.append( KanjiDictElem( col[0].strip(), col[1].strip(), col[2].strip() ) )
    
    # print dict  
    for elem in kanjidict:
      print "%s (%s): %s" % ( elem.kanji, elem.read, elem.english )
    
    # pick random elem
    rnd = kanjidict[ random.randint( 0, len(kanjidict)-1 ) ]
    
    # Q&A
    print "%s (%s): " % ( rnd.kanji,rnd.read )
    answer = raw_input()
    if answer == rnd.english:
      print "correct!"
    else:
      print "you get an F!"
    kanji_dict.txt:
    Code:
    九       |きゅう        |nine
    古       |ふるい        |old
    冒       |ぼう          |risk
    朋       |ほう          |companion
    明       |めい          |bright
    唱       |しょう        |chant
    晶       |しょう        |sparkle
    品       |しな          |goods
    呂       |りょ          |spine/ come together
    昌       |しょう        |prosperous
    早       |そう          |early
    旭       |きょく        |rising sun
    世       |せい          |generation
    胃       |い            |stomach
    旦       |たん          |nightbreak
    胆       |たん          |gall bladder
    亘       |こう          |span
    凹       |おう          |concave
    凸       |とつ          |convex
    output:
    Code:
    $ python -V
    Python 2.6.5
    $ ./kanji.py 
    九 (きゅう): nine
    古 (ふるい): old
    冒 (ぼう): risk
    朋 (ほう): companion
    明 (めい): bright
    唱 (しょう): chant
    晶 (しょう): sparkle
    品 (しな): goods
    呂 (りょ): spine/ come together
    昌 (しょう): prosperous
    早 (そう): early
    旭 (きょく): rising sun
    世 (せい): generation
    胃 (い): stomach
    旦 (たん): nightbreak
    胆 (たん): gall bladder
    亘 (こう): span
    凹 (おう): concave
    凸 (とつ): convex
    呂 (りょ): 
    spine/ come together
    correct!
    Last edited by Vaphell; October 20th, 2012 at 01:42 PM.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  4. #4
    Join Date
    Sep 2012
    Beans
    91

    Re: help make code shorter?

    thanks for the help guys! Just reading you're code make me wish I was as good as you guys I guess I'm going to have to practice a lot more!
    Last edited by snowlizard31; October 20th, 2012 at 08:16 PM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •