0

这是我在上一个问题中提到的与我的程序有关的另一个问题。我正在制作的程序是一个程序,它会以一种语言显示一个单词并要求翻译成另一种语言。然后它会显示它是对还是错。但是,当我测试它是否有效时,它会显示所有版本的不正确,即使答案是正确的。如何让它只显示适当的消息?例如,如果正确的说('Correct!'),如果有错误的说('不正确。正确的答案是_ ')

这就是我所拥有的:

if words == 'ano' and translation == 'um':
            print('Correct!')
        else:
            print('Incorrect.')
            print('The correct answer is: um.')

        if words == 'ima' and translation == 'now':
            print('Correct!')
        else:
            print('Incorrect.')
            print('The correct answer is: now.')

        if words == 'eego' and translation == 'english':
            print('Correct!')
        else:
            print('Incorrect.')
            print('The correct answer is: english.')

        if words == 'hai' and translation == 'yes':
            print('Correct!')
        else:
            print('Incorrect.')
            print('The correct answer is: yes.')

        if words == 'gakusei' and translation == 'student':
            print('Correct!')
        else:
            print('Incorrect.')
            print('The correct answer is: student.')

        if words == '...go' and translation == 'language':
            print('Correct!')
        else:
            print('Incorrect.')
            print('The correct answer is: language.')

        if words == 'kookoo' and translation == 'high school':
            print('Correct!')
        else:
            print('Incorrect.')
            print('The correct answer is: high school.')

        if words == 'gogo' and translation == 'pm':
            print('Correct!')
        else:
            print('Incorrect.')
            print('The correct answer is: pm.')

        if words == 'gozen' and translation == 'am':
            print('Correct!')
        else:
            print('Incorrect.')
            print('The correct answer is: am.')

        if words == '...sai' and translation == 'years old':
            print('Correct!')
        else:
            print('Incorrect.')
            print('The correct answer is: years old.')

        if words == '...san' and translation == 'mr/ms':
            print('Correct!')
        else:
            print('Incorrect.')
            print('The correct answer is: mr/ms.')

        if words == 'ji' and translation == "o'clock":
            print('Correct!')
        else:
            print('Incorrect.')
            print("The correct answer is: o'clock.")

        if words == 'jin' and translation == 'people':
            print('Correct.!')
        else:
            print('Incorrect.')
            print('The correct answer is: people.')

        if words == 'senkoo' and translation == 'major':
            print('Correct!')
        else:
            print('Incorrect.')
            print('The correct answer is: major.')

        if words == 'sensei' and translation == 'teacher':
            print('Correct!')
        else:
            print('Incorrect.')
            print('The correct answer is: teacher.')

        if words == 'soo desu' and translation == "that's right":
            print('Correct!')
        else:
            print('Incorrect.')
            print("The correct answer is: that's right.")

        if words == 'daigaku' and translation == 'college/university':
            print('Correct!')
        else:
            print('Incorrect.')
            print('The correct answer is: college/university.')

        if words == 'denwa' and translation == 'telephone':
            print('Correct!')
        else:
            print('Incorrect.')
            print('The correct answer is: telephone.')

        if words == 'tomodachi' and translation == 'friend':
            print('Correct.!')
        else:
            print('Incorrect.')
            print('The correct answer is: friend.')

        if words == 'namae' and translation == 'name':
            print('Correct!')
        else:
            print('Incorrect.')
            print('The correct answer is: name.')

        if words == 'nan/nani' and translation == 'what':
            print('Correct!')
        else:
            print('Incorrect.')
            print('The correct answer is: what.')

        if words == 'nihon' and translation == 'japan':
            print('Correct!')
        else:
            print('Incorrect.')
            print('The correct answer is: japan.')

        if words == '...nensei' and translation == 'year student':
            print('Correct!')
        else:
            print('Incorrect.')
            print('The correct answer is: year student.')

        if words == 'han' and translation == 'half':
            print('Correct!')
        else:
            print('Incorrect.')
            print('The correct answer is: half.')

        if words == 'bangoo' and translation == 'number':
            print('Correct!')
        else:
            print('Incorrect.')
            print('The correct answer is: number.')

        if words == 'ryuugakusei' and translation == 'international student':
            print('Ccorect!')
        else:
            print('Incorrect.')
            print('The correct answer is: international student.')

        if words == 'watashi' and translation == 'I':
            print('Correct!')
        else:
            print('Incorrect.')
            print('The correct answer is: I.')

        if words == 'amerika' and translation == 'america':
            print('Correct!')
        else:
            print('Incorrect.')
            print('The correct answer is: america.')

        if words == 'igirisu' and translation == 'britain':
            print('Correct!')
        else:
            print('Incorrect.')
            print('The correct answer is: britain.')

        if words == 'oosutoraria' and translation == 'australia':
            print('Correct!')
        else:
            print('Incorrect.')
            print('The correct answer is: australia.')

        if words == 'kankoku' and translation == 'korea':
            print('Correct!')
        else:
            print('Incorrect.')
            print('The correct answer is: korea.')

        if words == 'suweeden' and translation == 'sweeden':
            print('Correct!')
        else:
            print('Incorrect.')
            print('The correct answer is: sweeden.')

        if words == 'chuugoku' and translation == 'china':
            print('Correct!')
        else:
            print('Incorrect.')
            print('The correct answer is: china.')

        if words == 'kagaku' and translation == 'science':
            print('Correct!')
        else:
            print('Incorrect.')
            print('The correct answer is: science.')

        if words == 'ajia kenkyuu' and translation == 'asian studies':
            print('Correct!')
        else:
            print('Incorrect.')
            print('The correct answer is: asian studies.')

        if words == 'keizai' and translation == 'economics':
            print('Correct!')
        else:
            print('Incorrect.')
            print('The correct answer is: economics.')

        if words == 'kokusaikankei' and translation == 'international relations':
            print('Correct!')
        else:
            print('Incorrect.')
            print('The correct answer is: international relations.')

        if words == 'konpyuutaa' and translation == 'computer':
            print('Correct!')
        else:
            print('Incorrect.')
            print('The correct answer is: computer.')

        if words == 'jinruigaku' and translation == 'anthropology':
            print('Correct!')
        else:
            print('Incorrect.')
            print('The correct answer is: anthropology.')

        if words == 'seeji' and translation == 'politics':
            print('Correct!')
        else:
            print('Incorrect.')
            print('The correct answer is: politics.')        

        if words == 'bijinesu' and translation == 'business':
            print('Correct!')
        else:
            print('Incorrect.')
            print('The correct answer is: business.')

        if words == 'bungaku' and translation == 'literature':
            print('Correct!')
        else:
            print('Incorrect.')
            print('The correct answer is: literature.')

        if words == 'rekishi' and translation == 'history':
            print('Correct!')
        else:
            print('Incorrect.')
            print('The correct answer is: history.')

        if words == 'shigoto' and translation == 'job' or translation == 'work' or translation == 'occupation':
            print('Correct!')
        else:
            print('Incorrect.')
            print('The correct answer is: "job" or "work" or "occupation".')

        if words == 'isha' and translation == 'doctor':
            print('Correct!')
        else:
            print('Incorrect.')
            print('The correct answer is: doctor.')

        if words == 'kaishain' and translation == 'office worker':
            print('Correct!')
        else:
            print('Incorrect.')
            print('The correct answer is: office worker.')

        if words == 'kookoosei' and translation == 'high school student' or translation == 'highschool student':
            print('Correct!')
        else:
            print('Incorrect.')
            print('The correct answer is: "high school student" or "highschool student".')

        if words == 'shufu' and translation == 'house wife':
            print('Correct!')
        else:
            print('Incorrect.')
            print('The correct answer is: house wife.')

        if words == 'daigakuinsei' and translation == 'graduate student':
            print('Correct!')
        else:
            print('Incorrect.')
            print('The correct answer is: graduate student.')

        if words == 'daigakusei' and translation == 'college student':
            print('Correct!')
        else:
            print('Incorrect.')
            print('The correct answer is: college student.')

        if words == 'bengoshi' and translation == 'lawyer':
            print('Correct!')
        else:
            print('Incorrect.')
            print('The correct answer is: lawyer.')

        if words == 'okaasan' and translation == 'mother' or translation == 'mom':
            print('Correct!')
        else:
            print('Incorrect.')
            print('The correct answer is: "mother or "mom".')

        if words == 'otoosan' and translation == 'father' or translation == 'dad':
            print('Correct!')
        else:
            print('Incorrect.')
            print('The correct answer is: "father or "dad".')

        if words == 'oneesan' and translation == 'older sister' or translation == 'big sister':
            print('Correct!')
        else:
            print('Incorrect.')
            print('The correct answer is: "older sister" or "big sister".')

        if words == 'oniisan' and translation == 'older brotehr' or translation == 'big brother':
            print('Correct!')
        else:
            print('Incorrect.')
            print('The correct answer is: "older brother" or "big brother".')

        if words == 'imooto' and translation == 'younger sister' or translation == 'little sister':
            print('Correct!')
        else:
            print('Incorrect.')
            print('The correct answer is: "younger sister" or "little sister".')

        if words == 'otooto' and translation == 'younger brother' or translation == 'little brother':
            print('Correct!')
        else:
            print('Incorrect.')
            print('The correct answer is: "younger brother" or "little brother".')

编辑:这是上述代码之前的代码。不确定它是否会有所帮助。

words = ['ano', 'ima', 'eego', 'hai', 'gakusei', '...go', 'kookoo', 'gogo', 'gozen', '...sai', '...san', '...ji', '...jin', 'senkoo', 'sensei', 'soo desu', 'daigaku', 'denwa', 'tomodachi', 'namae', 'nan/nani', 'nihon', '...nensei', 'han', 'bangoo', 'ryuugakusei', 'watashi', 'amerika', 'igirisu', 'oosutoraria', 'kankoku', 'suweeden', 'chuugoku', 'kagaku', 'ajia kenkyuu', 'keizai', 'kokusaikankei', 'konpyuutaa', 'jinruigaku', 'seeji', 'bijinesu', 'bungaku', 'rekishi', 'shigoto', 'isha', 'kaishain', 'kookoosei', 'shufu', 'daigakuinsei', 'bengoshi', 'okaasan', 'otoosan', 'oneesan', 'oniisan', 'imooto', 'otooto']
    random.shuffle(words)

    index = 0
    while index < len(words):
        print(words[index])
        translation = input('Enter the translation: ')
        index += 1
        print() 
4

3 回答 3

2

下面的代码不是更容易维护吗?

the_words = {'gakusei':'student',
             'hai':'yes',
             '...go':'language',
             'kookoo':'high school',
            }


the_word = 'GAKUSEI'
translation = 'Student'

the_word_lcase = the_word.lower()
if not the_word_lcase in the_words:
    print 'Unknown word: %s' % the_word
elif the_words[the_word_lcase] == translation.lower():
    print 'Correct!'
else:
    print 'The correct answer is: %s' % the_words[the_word_lcase];

在测试之前,我对其进行了编辑以使单词小写。只要您的主列表(the_words 字典)全部为小写,代码现在将忽略大小写。这当然取决于您希望验证的严格程度。

于 2012-11-27T20:55:48.510 回答
1

这是很多重复的代码。以下是一个框架,您可以基于该框架使用 adict来存储翻译(因此它们保存在一个地方而不是if语句中),并且可以说不正确/正确的翻译是什么,以及当一个词不被认可。

translations = {
    'ano': 'um',
    'ima': 'now',
    'eego': 'english'
}

some_word = 'ano'

try:
    translated = translations[some_word]
    if translation == translated:
        print('Correct!')
    else:
        print('Ooops, that should be:', translated)
except KeyError as e:
    print("Sorry, I don't know", some_word)
于 2012-11-27T20:55:37.370 回答
0

Nest your comparisons.

    if words == 'ima':
      if translation == 'now':
        print('Correct!')
      else:
        print('Incorrect.')
        print('The correct answer is: now.')
于 2012-11-27T20:47:39.143 回答