2012-12-13 77 views
-1

我幾乎完成了這個程序,我正在幫助我爲我的日語課程學習。我需要做的就是弄清楚如何跟蹤我有多少錯誤,然後打印結果。我的計劃是有點大,但是這是我(還沒有工作的方式我想它)Python得分計數器

RIGHT = 0 
WRONG = 0 

import random 

def main():  
    study_more = 'y' 

    while study_more == 'y': 
     start() 

     score() 

     study_more = input('Do you want to study more? (y for yes): ') 

def start(): 
    answer2 = int(input('Enter the chapter you would like to study (1-6): ')) 
    answer3 = int(input('Japanese to English? (1) or English to Japanese (2): ')) 
    print() 

    if answer2 == 1 and answer3 == 1: 
     ch1_japanese_to_english() 
    elif answer2 == 1 and answer3 == 2: 
     ch1_english_to_japanese() 
    elif answer2 == 2 and answer3 == 1: 
     ch2_japanese_to_english() 
    elif answer2 == 2 and answer3 == 2: 
     ch2_english_to_japanese() 
    elif answer2 == 3 and answer3 == 1: 
     ch3_japanese_to_english() 
    elif answer2 == 3 and answer3 == 2: 
     ch3_english_to_japanese() 
    elif answer2 == 4 and answer3 == 1: 
     ch4_japanese_to_english() 
    elif answer2 == 4 and answer3 == 2: 
     ch4_english_to_japanese() 
    elif answer2 == 5 and answer3 == 1: 
     ch5_japanese_to_english() 
    elif answer2 == 5 and answer3 == 2: 
     ch5_english_to_japanese() 
    elif answer2 == 6 and answer3 == 1: 
     ch6_japanese_to_english() 
    elif answer2 == 6 and answer3 == 2: 
     ch6_english_to_japanese() 
    else: 
     print("You've entered invalid responses.") 

def score(): 

    ch1_japanese_to_english() 
    ch1_english_to_japanese()  
    ch2_japanese_to_english() 
    ch2_english_to_japanese() 
    ch3_japanese_to_english() 
    ch3_english_to_japanese() 
    ch4_japanese_to_english() 
    ch4_english_to_japanese() 
    ch5_japanese_to_english() 
    ch5_english_to_japanese() 
    ch6_japanese_to_english() 
    ch6_english_to_japanese() 

    print('The number you got right: ', RIGHT) 
    print('The number you go wrong: ', WRONG) 

def ch1_japanese_to_english(): 
    dictionary = {'ano' : 'um', 'ima': 'now', 'eego' : 'english', 'hai' : 'yes', 'gakusei' : 'student', '...go' : 'language', 'kookoo' : 'high school', 'gogo' : 'pm', 'gozen' : 'am', '...sai' : 'years old', '...san' : 'mr/ms', '...ji' : "o'clock", '...jin' : 'people', 'senkoo' : 'major', 'sensei' : 'teacher', 'soo desu' : "that's right", 'daigaku' : 'college', 'denwa' : 'telephone', 'tomodachi' : 'friend', 'namae' : 'name', 'nani' : 'what', 'nihon' : 'japan', '...nensei' : 'year student', 'han' : 'half', 'bangoo' : 'number', 'ryuugakusei' : 'international student', 'watashi' : 'I', 'amerika' : 'america', 'igirisu' : 'britain', 'oosutoraria' : 'australia', 'kankoku' : 'korea', 'suweeden' : 'sweeden', 'chuugoku' : 'china', 'kagaku': 'science', 'ajia kenkyuu' : 'asian studies', 'keizai' : 'economics', 'kokusaikankei' : 'international relations', 'konpyuutaa' : 'computer', 'jinruigaku' : 'anthropology', 'seeji' : 'politics', 'bijinesu' : 'business', 'bungaku' : 'literature', 'rekishi' : 'history', 'shigoto' : 'occupation', 'isha' : 'doctor', 'kaishain' : 'office worker', 'kookoosei' : 'high school student', 'shufu' : 'house wife', 'daigakuinsei' : 'graduate student', 'bengoshi' : 'lawyer', 'okaasan' : 'mother', 'otoosan' : 'father', 'oneesan' : 'older sister', 'oniisan' : 'older brother', 'imooto' : 'younger sister', 'otooto': 'younger brother'} 
    words = list(dictionary.items()) 
    random.shuffle(words) 

    for word, translation in words: 
     print() 
     print(word) 
     answer = input('Enter the translation: ') 

     if answer == translation: 
      print('Correct!') 
      RIGHT += 1 
     else: 
      print('Incorrect.') 
      print('The correct answer is: %s.' % translation) 
      WRONG += 1 

     print() 
     return RIGHT 
     return WRONG 

def ch1_english_to_japanese(): 
    dictionary = {'um' : 'ano', 'now' : 'ima', 'english' : 'eego', 'yes' : 'hai', 'student' : 'gakusei', 'language' : 'go', 'high school' : 'kookoo', 'pm': 'gogo', 'am' : 'gozen', 'years old': 'sai', 'mr/ms' : 'san', "o'clock" : 'ji', 'people' : 'go', 'major' : 'senkoo', 'teacher' : 'sensei', "that's right" : 'soo desu', 'college' : 'daigaku', 'telephone' : 'denwa', 'friend' : 'tomodachi', 'name' : 'namae', 'what' : 'nani', 'japan' : 'nihon', 'year student' : 'nensei', 'half' : 'han', 'number' : 'bangoo', 'international student' : 'ryuugakusei', 'I' : 'watashi', 'america' : 'amerika', 'britain' : 'igirisu' , 'australia' : 'oosutoraria', 'korea' : 'kankoku', 'sweeden' : 'suweeden', 'china' : 'chuugoku', 'science' : 'kagaku', 'asian studies' : 'ajia kenkyuu', 'economics' : 'keizai', 'international relations' : 'kokusaikankei', 'computer' : 'konpyuutaa', 'anthropology' : 'jinruigaku', 'politics' : 'seeji', 'business' : 'bijinesu', 'literature' : 'bungaku', 'history' : 'rekishi', 'occupation' : 'shigoto', 'doctor' : 'isha', 'office worker' : 'kaishain', 'high school student' : 'kookoosei', 'house wife' : 'shufu', 'graduate student' : 'daigakuinsei', 'lawyer' : 'bengoshi', 'mother' : 'okaasan', 'father' : 'otoosan', 'older sister' : 'oneesan', 'older brother' : 'oniisan', 'younger sister' : 'imooto', 'younger brother' : 'otooto'} 
    words = list(dictionary.items()) 
    random.shuffle(words) 

    for word, translation in words: 
     print() 
     print(word) 
     answer = input('Enter the translation: ') 

     if answer == translation: 
      print('Correct!') 
      RIGHT += 1 
     else: 
      print('Incorrect.') 
      print('The correct answer is: %s.' % translation) 
      WRONG += 1 

     print() 
     return RIGHT 
     return WRONG   

def ch2_japanese_to_english(): 
    dictionary = {'kore' : 'this one', 'sore' : 'that one', 'are' : 'that one over there', 'dore' : 'which one', 'kono' : 'this', 'sono' : 'that', 'ano' : 'that over there', 'dono' : 'which', 'koko' : 'here', 'soko' : 'there', 'asoko' : 'over there', 'doko' : 'where', 'dare' : 'who', 'oishii' : 'delicious', 'sakana' : 'fish', 'tonkatsu' : 'pork cutlet', 'niku' : 'meat', 'menyuu' : 'menu', 'yasai' : 'vegetable', 'enpitsu' : 'pencil', 'kasa' : 'umbrella', 'kaban' : 'bag', 'kutsu' : 'shoes', 'saifu' : 'wallet', 'jiinzu' : 'jeans', 'jisho' : 'dictionary', 'jitensha' : 'bicycle', 'shinbun' : 'newspaper', 'tiishatsu' : 't-shirt', 'tokei' : 'clock', 'nooto' : 'notebook', 'pen' : 'pen', 'booshi' : 'hat', 'hon' : 'book', 'kissaten' : 'cafe', 'ginkoo' : 'bank', 'toire' : 'restroom', 'toshokan' : 'library', 'yuubinkyoku' : 'post office', 'ikura' : 'how much', 'en' : 'yen', 'takai' : 'expensive', 'irasshaimase' : 'welcome', 'onegaishimasu': 'please', 'kudasai' : 'please give me', 'jaa' : 'then', 'doozo' : 'here it is', 'doomo' : 'thank you'} 
    words = list(dictionary.items()) 
    random.shuffle(words) 

    for word, translation in words: 
     print() 
     print(word) 
     answer = input('Enter the translation: ') 

     if answer == translation: 
      print('Correct!') 
      RIGHT += 1 
     else: 
      print('Incorrect.') 
      print('The correct answer is: %s.' % translation) 
      WRONG += 1 

     print() 
     return RIGHT 
     return WRONG   

def ch2_english_to_japanese(): 
    dictionary = {'this one' : 'kore', 'that one' : 'sore', 'that one over there' : 'are', 'which one' : 'dore', 'this' : 'kono', 'that' : 'sono', 'that over there' : 'ano', 'which' : 'dono', 'here' : 'koko', 'there' : 'soko', 'over there' : 'asoko', 'where' : 'doko', 'who' : 'dare', 'delicious' : 'oishii', 'fish' : 'sakana', 'pork cutlet' : 'tonkatsu', 'meat' : 'niku', 'menu' : 'menyuu', 'vegetable' : 'yasai', 'pencil' : 'enpitsu', 'umbrella' : 'kasa', 'bag' : 'kaban', 'shoes' : 'kutsu', 'wallet' : 'saifu', 'jeans' : 'jiinzu', 'dictionary' : 'jisho', 'bicycle' : 'jitensha', 'newspaper' : 'shinbun', 't-shirt' : 'tiishatsu', 'clock' : 'tokei', 'notebook' : 'nooto', 'pen' : 'pen', 'hat' : 'booshi', 'book' : 'hon', 'cafe' : 'kissaten', 'bank' : 'ginkoo', 'restroom' : 'toire', 'library' : 'toshokan', 'post office' : 'yuubinkyoku', 'how much' : 'ikura', 'yen' : 'en', 'expensive' : 'takai', 'welcome' : 'irasshaimase', 'please' : 'onegaishimasu', 'please give me' : 'kudasai', 'then' : 'jaa', 'here it is' : 'doozo', 'thank you' : 'doomo'} 
    words = list(dictionary.items()) 
    random.shuffle(words) 

    for word, translation in words: 
     print() 
     print(word) 
     answer = input('Enter the translation: ') 

     if answer == translation: 
      print('Correct!') 
      RIGHT += 1 
     else: 
      print('Incorrect.') 
      print('The correct answer is: %s.' % translation) 
      WRONG += 1 

     print() 
     return RIGHT 
     return WRONG   

def ch3_japanese_to_english(): 
    dictionary = {'eiga' : 'movie','ongaku' : 'music','zasshi' : 'magazine','supootsu' : 'sports','deeto' : 'date','tenisu' : 'tenis','terebi' : 'tv','aisukuriimu' : 'ice cream','asagohan' : 'breakfast','osake' : 'alchohol','ocha' : 'green tea','koohii' : 'coffee','bangohan' : 'dinner','hanbaagaa' : 'hamburger','hirugohan' : 'lunch','mizu' : 'water','ie' : 'home','uchi' : 'my place','gakkoo' : 'school','asa' : 'morning','ashite' : 'tommorow','itsu' : 'when','kyoo' : 'today','goro' : 'at about','konban' : 'tonight','shuumatsu' : 'weekend','doyoobi' : 'saturday','nichiyoobi' : 'sunday','mainichi' : 'everyday','maiban' : 'every night','iku' : 'to go','kaeru' : 'to return','kiku' : 'to listen','nomu' : 'to drink','hanasu' : 'to speak','yomu' : 'to read','okiru' : 'to get up','taberu' : 'to eat','neru' : 'to sleep','miru' : 'to see','kuru' : 'to come','suru' : 'to do','benkyoosuru' : 'to study','ii' : 'good','hayai' : 'early','amari' : 'not much','zenzen' : 'not at all','taitei' : 'usually','chotto' : 'a little','tokidoki' : 'sometimes','yoku' : 'often','soo desu ne' : "that's right",'demo' : 'but','doo desu ka' : 'how about'} 
    words = list(dictionary.items()) 
    random.shuffle(words) 

    for word, translation in words: 
     print() 
     print(word) 
     answer = input('Enter the translation: ') 

     if answer == translation: 
      print('Correct!') 
      RIGHT += 1 
     else: 
      print('Incorrect.') 
      print('The correct answer is: %s.' % translation) 
      WRONG += 1 

     print() 
     return RIGHT 
     return WRONG   

def ch3_english_to_japanese(): 
    dictionary = {'movie' : 'eiga','music' : 'ongaku','magazine' : 'zasshi','sports' : 'supootsu','date' : 'deeto','tenis' : 'tenisu','tv' : 'terebi','ice cream' : 'aisukuriimu','breakfast' : 'asagohan','alchohol' : 'osake','green tea' : 'ocha','coffee' : 'koohii','dinner' : 'bangohan','hamburger' : 'hanbaagaa','lunch' : 'hirugohan','water' : 'mizu','home' : 'ie','my place' : 'uchi','school' : 'gakkoo','morning' : 'asa','tommorow' : 'ashite','when' : 'itsu','today' : 'kyoo','at about' : 'goro','tonight' : 'konban','weekend' : 'shuumatsu','saturday' : 'doyoobi','sunday' : 'nichiyoobi','everyday' : 'mainichi','every night' : 'maiban','to go' : 'iku','to return' : 'kaeru','to listen' : 'kiku','to drink' : 'nomu','to speak' : 'hanasu','to read' : 'yomu','to get up' : 'okiru','to eat' : 'taberu','to sleep' : 'neru','to see' : 'miru','to come' : 'kuru','to do' : 'suru','to study' : 'benkyoosuru','good' : 'ii','early' : 'hayai','not much' : 'amari','not at all' : 'zenzen','usually' : 'taitei','a little' : 'chotto','sometimes' : 'tokidoki','often' : 'yoku',"that's right" : 'soo desu ne','demo' : 'but','doo desu ka' : 'how about'} 
    words = list(dictionary.items()) 
    random.shuffle(words) 

    for word, translation in words: 
     print() 
     print(word) 
     answer = input('Enter the translation: ') 

     if answer == translation: 
      print('Correct!') 
      RIGHT += 1 
     else: 
      print('Incorrect.') 
      print('The correct answer is: %s.' % translation) 
      WRONG += 1 

     print() 
     return RIGHT 
     return WRONG   

def ch4_japanese_to_english(): 
    dictionary = {'arubaito' : 'part time job','kaimono' : 'shopping','kurasu' : 'class','anata' : 'you','isu' : 'chair','inu' : 'dog','amiyage' : 'souvenir','kodomo' : 'child','gohan' : 'meal','shashin' : 'picture','tsukue' : 'desk','tegami' : 'letter','neko' : 'cat','paso' : 'bread','hito' : 'person','meeru' : 'e-mail','otera' : 'temple','kooen' : 'park','suupaa' : 'supermarket','depaato' : 'department store','basutei' : 'bus stop','byooin' : 'hospital','hoteru' : 'hotel','honya' : 'bookstore','machi' : 'city','resutoran' : 'restaurant','kinoo' : 'yesterday','jikan' : 'hours','ichijikan' : 'one hour','senshuu' : 'last week','toki' : 'when','getsuyoobi' : 'monday','kayoobi' : 'tuesday','suiyoobi' : 'wednesday','mokuyoobi' : 'thursday','kinyoobi' : 'friday','au' : 'to meet','aru' : 'there is','kau' : 'to buy','kaku' : 'to write','toru' : 'to take','matsu' : 'to wait','wakaru' : 'to understand','iru' : 'is in','gurai' : 'about','gomennasai' : "I'm sorry",'dakara' : 'so','takusan' : 'many','to' : 'together with','dooshite' : 'why','hitoride' : 'alone','migi' : 'right','hidari' : 'left','mae' : 'front', 'ushiro' : 'back', 'naka' : 'inside', 'ue' : 'on', 'shita' : 'under', 'chikaku' : 'near', 'tonari' : 'next', 'aida' : 'between'} 
    words = list(dictionary.items()) 
    random.shuffle(words) 

    for word, translation in words: 
     print() 
     print(word) 
     answer = input('Enter the translation: ') 

     if answer == translation: 
      print('Correct!') 
      RIGHT += 1 
     else: 
      print('Incorrect.') 
      print('The correct answer is: %s.' % translation) 
      WRONG += 1 

     print() 
     return RIGHT 
     return WRONG   

def ch4_english_to_japanese(): 
    dictionary = {'part time job' : 'arubaito','shopping' : 'kaimono','class' : 'kurasu','you' : 'anata','chair' : 'isu','dog' : 'inu','souvenir' : 'omiyage','child' : 'kodomo','meal' : 'gohan','picture' : 'shashin','desk' : 'tsukue','letter' : 'tegami','cat' : 'neko','bread' : 'paso','person' : 'hito','e-mail' : 'meeru','temple' : 'otera','park' : 'kooen','supermarket' : 'suupaa','department store' : 'depaato','bus stop' : 'basutei','hospital' : 'byooin','hotel' : 'hoteru','bookstore' : 'honya','city' : 'machi','restaurant' : 'resutoran','yesterday' : 'kinoo','hours' : 'jikan','one hour' : 'ichijikan','last week' : 'senshuu','when' : 'toki','monday' : 'getsuyoobi','tuesday' : 'kayoobi','wednesday' : 'suiyoobi','thursday' : 'mokuyoobi','friday' : 'kinyoobi','to meet' : 'au','there is' : 'aru','to buy' : 'kau','to write' : 'kaku','to take' : 'toru','to wait' : 'matsu','to understand' : 'wakaru','is in' : 'iru','about' : 'gurai',"I'm sorry" : 'gomennasai','so' : 'dakara','many' : 'takusan','together with' : 'to','why' : 'dooshite','alone' : 'hitoride','right' : 'migi','left' : 'hidari','front' : 'mae', 'back' : 'ushiro', 'inside' : 'naka', 'on' : 'ue', 'under' : 'shita', 'near' : 'chikaku', 'next' : 'tonari', 'between' : 'aida'} 
    words = list(dictionary.items()) 
    random.shuffle(words) 

    for word, translation in words: 
     print() 
     print(word) 
     answer = input('Enter the translation: ') 

     if answer == translation: 
      print('Correct!') 
      RIGHT += 1 
     else: 
      print('Incorrect.') 
      print('The correct answer is: %s.' % translation) 
      WRONG += 1 

     print() 
     return RIGHT 
     return WRONG   

def ch5_japanese_to_english(): 
    dictionary = {'umi' : 'sea','kitte' : 'postal stamps','kippu' : 'ticket','saafuin' : 'surfing','shukudai' : 'homework','tabemono' : 'food','tanjoobi' : 'birthday','tesuto' : 'test','tenki' : 'weather','nomimono' : 'drink','hagaki' : 'post card','basu' : 'bus','hikooki' : 'airplane','heya' : 'room','boku' : 'I','yasumi' : 'holiday','ruogoo' : 'travel','atarashii' : 'new','atsui' : 'hot','isogashii' : 'busy','ookii' : 'large','omoshiroi' : 'interesting','kakkoii' : 'good looking','kowai' : 'frightening','samui' : 'cold','tanoshii' : 'fun','chiisai' : 'small','tsumaranai' : 'boring','furui' : 'old','muzukashii' : 'difficult','yasashii' : 'easy','yasui' : 'cheap','kirai' : 'to dislike','kirei' : 'beautiful','genki' : 'healthy','shizuka' : 'quiet','suki' : 'to like','daikirai' : 'to hate','daisuki' : 'to love','nigiyaka' : 'lively','hima' : 'not busy','oyogu' : 'to swim','kiku' : 'to ask','noru' : 'to ride','yaru' : 'to do','dekakeru' : 'to go out','isshoni' : 'together','sugoku' : 'extreamly','sorekara' : 'and then','daijoobu' : "it's okay",'totemo' : 'very','donna' : 'what kind of'} 
    words = list(dictionary.items()) 
    random.shuffle(words) 

    for word, translation in words: 
     print() 
     print(word) 
     answer = input('Enter the translation: ') 

     if answer == translation: 
      print('Correct!') 
      RIGHT += 1 
     else: 
      print('Incorrect.') 
      print('The correct answer is: %s.' % translation) 
      WRONG += 1 

     print() 
     return RIGHT 
     return WRONG   

def ch5_english_to_japanese(): 
    dictionary = {'sea' : 'umi','postal stamps' : 'kitte','ticket' : 'kippu','surfing' : 'saafuin','homework' : 'shukudai','food' : 'tabemono','birthday' : 'tanjoobi','test' : 'tesuto','weather' : 'tenki','drink' : 'nomimono','post card' : 'hagaki','bus' : 'basu','airplane' : 'hikooki','room' : 'heya','I' : 'boku','holiday' : 'yasumi','travel' : 'ryogoo','new' : 'atarashii','hot' : 'atsui','busy' : 'isogashii','large' : 'ookii','interesting' : 'omoshiroi','good looking' : 'kakkoii','frightening' : 'kowai','cold' : 'samui','fun' : 'tanoshii','small' : 'chiisai','boring' : 'tsumaranai','old' : 'furui','difficult' : 'muzukashii','easy' : 'yasashii','chaep' : 'yasui','to dislike' : 'kirai','beautiful' : 'kirei','healthy' : 'genki','quiet' : 'shizuka','to like' : 'suki','to hate' : 'dakirai','to love' : 'daisuki','lively' : 'nigiyaka','not busy' : 'hima','to swim' : 'oyogu','to ask' : 'kiku','to ride' : 'noru','to do' : 'yaru','to go out' : 'dekakeru','together' : 'isshoni','extreamly' : 'sugoku','and then' : 'sorekara',"it's okay" : 'daijoobu','very' : 'totemo','what kind of' : 'donna'} 
    words = list(dictionary.items()) 
    random.shuffle(words) 

    for word, translation in words: 
     print() 
     print(word) 
     answer = input('Enter the translation: ') 

     if answer == translation: 
      print('Correct!') 
      RIGHT += 1 
     else: 
      print('Incorrect.') 
      print('The correct answer is: %s.' % translation) 
      WRONG += 1 

     print() 
     return RIGHT 
     return WRONG   

def ch6_japanese_to_english(): 
    dictionary = {'okane' : 'money','ofuro' : 'bath','kanji' : 'kanji','kyookasho' : 'textbook','konshuu' : 'this weeek','shiidii' : 'cd','shiminbyooin' : 'municipal hospital','shiyawaa' : 'shower','tsugi' : 'next','denki' : 'electricity','densha' : 'train','nimotsu' : 'baggage','pasokon' : 'personal computer','peeji' : 'page','mado' : 'window','yoru' : 'night','raishuu' : 'next week','rainen' : 'next year','taihen' : 'tough','asobu' : 'to play','isogu' : 'to hurry','ofuronihairu' : 'to take a bath','kaesu' : 'to return','kesu' : 'to turn off','shinu' : 'to die','suwaru' : 'to sit down','tatsu' : 'to stand up','tabaiwosuu' : 'to smoke','tsukau' : 'to use','tetsudau' : 'to help','hairu' : 'to enter','motsu' : 'to carry','yasumu' : 'to be absent','akeru' : 'to open','oshieru' : 'to teach','oriru' : 'to get off','kariru' : 'to borrow','shinuru' : 'to close','shiyawaawoabiru' : 'to take a shower','to turn on' : 'tsukeru','to make a phone call' : 'denwawokakeru','wasureru' : 'to forget','tsuretekuru' : 'to bring a person','mottekuru' : 'to bringa thing','atode' : 'later on','osoku' : 'do something late','kara' : 'because','kekkoo desu' : 'that would be fine','sugu' : 'right away','honto desu ka' : 'really?','yukkuri' : 'slowly'} 
    words = list(dictionary.items()) 
    random.shuffle(words) 

    for word, translation in words: 
     print() 
     print(word) 
     answer = input('Enter the translation: ') 

     if answer == translation: 
      print('Correct!') 
      RIGHT += 1 
     else: 
      print('Incorrect.') 
      print('The correct answer is: %s.' % translation) 
      WRONG += 1 

     print() 
     return RIGHT 
     return WRONG   

def ch6_english_to_japanese(): 
    dictionary = {'money' : 'okane','bath' : 'ofuro','kanji' : 'kanji','textbook' : 'kyookasho','this week' : 'konshuu','cd' : 'shiidii','municipal hospital' : 'shiminbyooin','shower' : 'shiyawaa','next' : 'tsugi','electricity' : 'denki','train' : 'densha','baggage' : 'nimotsu','personal computer' : 'pasokon','page' : 'peeji','window' : 'mado','night' : 'yoru','next week' : 'raishuu','next year' : 'rainen','tough' : 'taihen','to play' : 'asobu','to hurry' : 'isogu','to take a bath' : 'ofuronihairu','to return' : 'kaesu','to turn off' : 'kesu','to die' : 'shinu','to sit down' : 'suwaru','to stand up' : 'tatsu','to smoke' : 'tabaiwosuu','to use' : 'tsukau','to help' : 'tetsudau','to enter' : 'hairu','to carry' : 'motsu','to be absent' : 'yasumu','to open' : 'akeru','to teach' : 'oshieru','to get off' : 'oriru','to borrow' : 'kariru','to close' : 'shinuru','to take a shower' : 'shiyawaawoabiru','to turn on' : 'tsukeru','to make a phone call' : 'denwawokakeru','to forget' : 'wasureru','to bring a person' : 'tsuretekuru','to bring a thing' : 'mottekuru','later on' : 'atode','do something late' : 'osoku','because' : 'kara','that would be fine' : 'kekkoo desu','right away' : 'sugu','really?' : 'hontoo desu ka','slowly' : 'yukkuri'} 
    words = list(dictionary.items()) 
    random.shuffle(words) 

    for word, translation in words: 
     print() 
     print(word) 
     answer = input('Enter the translation: ') 

     if answer == translation: 
      print('Correct!') 
      RIGHT += 1 
     else: 
      print('Incorrect.') 
      print('The correct answer is: %s.' % translation) 
      WRONG += 1 

     print() 
     return RIGHT 
     return WRONG   

main() 
+3

sooo很多代碼重複... –

+0

你能比「不按我想要的方式工作嗎?」更具體嗎?這裏有很多代碼。向我們展示相關部分,您的期望以及它在做什麼。 –

+0

對於不同的章節和詞彙,它基本上是相同的程序。我只需要幫助保持分數 –

回答

3

我知道這是不是真的,你問什麼,所以道歉,如果這是一個實際的任務,在這種情況下,這可能不會有幫助。正如其他人所說,代碼中有很多重複。突出的事情是:基本相同的功能,處理實際測驗和(在較小程度上)解釋響應所涉及的陳述的數量。以下代碼的總體思路是將所有這些縮減爲一個Quiz函數,並將所有測驗詞存儲在不同的模塊中。關於章的話,因爲章詞典實際上是相同的,只是顛倒,你可以使用:

words = {v: k for k, v in words.items()} 

到「反向」它的情況下,你想從英國去 - >日本。然後,一旦完成,分數就可以打印出來。這可以進一步優化,但希望這將讓你開始:

import random 
import quiz_vocab 

CHAPTER_WORDS = { 
    1: quiz_vocab.chapter_1, 
    2: quiz_vocab.chapter_2 
    } 


def main(): 
    study_more = 'y' 
    right = 0 
    wrong = 0 

    while study_more == 'y': 
     chapter = int(input('Enter the chapter you would like to study (1-6): ')) 
     direction = int(input('Japanese to English? (1) or English to Japanese (2): ')) 

     # Do additional handling here to make sure they pick the right stuff 
     if chapter not in CHAPTER_WORDS or direction not in (1, 2): 
     print("You've entered invalid responses.") 
     continue 
     print() 

     # Return the results of quiz into their respective variables 
     # and increment the overall totals 
     num_right, num_wrong = quiz(chapter, direction) 
     total_right += num_right 
     total_wrong += num_wrong 

     # Cycle again 
     study_more = input('Do you want to study more? (y for yes): ') 

    # Print the final total 
    print('Right: {0}\nWrong: {1}'.format(total_right, total_wrong)) 


def quiz(chapter, direction): 
    right, wrong = 0, 0 

    # Get the words corresponding to the input 
    words = CHAPTER_WORDS[chapter] 

    # If we decide to go English -> Japanese, reverse the dictionary 
    if direction == 2: 
     words = {v: k for k, v in words.items()} 

    # This is all the same as yours... 
    words = list(words.items()) 
    random.shuffle(words) 

    for word, translation in words: 
     print() 
     print(word) 
     answer = input('Enter the translation: ') 

     if answer == translation: 
      print('Correct!') 
      right += 1 
     else: 
      print('Incorrect.') 
      print('The correct answer is: %s.' % translation) 
      wrong += 1 

    # Return the right/wrong counts 
    return right, wrong 

main() 

而且quiz_vocab模塊的樣子:

chapter_1 = {'ano' : 'um', 'ima': 'now', 'eego' : 'english'} # continues... 
+0

很好的答案,但我建議看看代碼樣式的[PEP 8](http://www.python.org/dev/peps/pep-0008/)。 (Python變量應該是小寫,CamelCase是爲類保留的,而4個空格塊是標準的。) –

+0

@David Ha,更糟糕的是,使它符合PEP 8是我通常花費最多的時間:)我最初將盡可能地保持與OP的代碼接近,但最終會出現上面的拼湊。另外,我的Vim是默認的兩個空格縮進 - 各種各樣的混亂:) – RocketDonkey

0

有許多與此代碼的問題。

  1. 冗餘代碼:作爲@Joran指出,有重複代碼的大量信息。函數應該是可推廣。每個功能都要求用戶輸入,然後檢查它是否與字典的存儲值相匹配。唯一改變的就是字典。爲什麼不做一個「測驗」功能,並通過字典?
  2. 全局變量:非常不好。我會把它留給你來研究爲什麼,但是你的功能不應該改變一個全球的正確/錯誤的數量。
  3. 詳細代碼:巨大的if/else塊是非pythonic代碼的標誌。考慮遵循您的previous question中給出的建議。

結合這一切,你的全身quizzing功能應該是這個樣子:

def quiz(word_dict): 
    """ Quiz the user on a word dictionary, returning right/wrong count. """ 
    right = wrong = 0 # Initialize local count 
    words = list(word_dict.items()) 
    random.shuffle(words) 

    for word, translation in words: 
     print(word) 
     answer = input('Enter the translation: ') 
     # (...) 
    return (right, wrong) 

一個quizzing會議應包括提示用戶的章節,選擇合適的字典,然後調用quizzing功能。

1

我直接看到的一個錯誤是變量RIGHTWRONG在您的'章節'功能中被賦值之前被引用。每個函數必須包括global RIGHT, WRONG

而且使你的程序相當小,你可以採用以下start函數中:

answer1 = raw_input('Enter the chapter you would like to study (1-6): ') 
answer2 = input('Japanese to English? (1) or English to Japanese (2): ') 

if answer1 in range(1,7) and answer2 in [1,2]: 
    functionName = 'ch{}_{}()'.format(answer1, ['japanese_to_english', 'english_tojapanese'][answer2-1]) 
    eval(functionName) 

這將替換所有if elif條款


還有一個改進我可以建議的是,不是每個章節都有一個函數,你可以有一個所有字典的列表(因爲它們是每個'章節'函數中唯一不同的東西)所有功能按順序排列。然後只有一個函數接受answer1answer2作爲參數,並相應地選擇所需的字典。例如:

j_to_e = [ # all the japanese to english dictionaries in order as a list] 
e_to_j = [ # all the english to japanese dictionaries in order as a list] 
all_dicts = [j_to_e, e_to_j] 

def chapter(chapterNumber, j_to_e): 
    global RIGHT, WRONG 
    curDict = all_dicts[j_to_e-1][chapterNumber-1] # '-1' to compensate 0 based lists 
    words = list(curDict.items()) 
    random.shuffle(words) 

    for word, translation in words: 
     # all the function code 
     ... 
     print() 
     return RIGHT, WRONG # RIGHT and WRONG should be returned on the same line otherwise 'return WRONG' won't be executed 

上述改進將使您的代碼顯着簡潔。 希望這有助於。

+0

由@RocketDonkey建議,因爲'j_to_e'和'e_to_j'字典相互顛倒,你可以通過使用just其中之一併根據需要操縱它們。第一次我沒有注意到它們的相似性。 –

2

在我看來,您似乎希望用戶選擇要運行的測試(在start()),但是然後在score()之內再次運行所有測試。

如果您正在跟蹤全局變量中正確/錯誤答案的數量(不應該這樣做,但這是一個不同的問題),那麼score()函數應該只打印全局變量的值。

順便說一句,當您直接修改全局變量時,測試函數不需要返回變量的值。無論如何,雙重回報聲明是沒有意義的。該函數返回第一個語句,永遠不會到第二個語句。