外语翻译词典

foreign language translation dictionary

我正在尝试根据我学过的 Python 个新单词编一本字典。然后,该程序将通过要求我翻译字典中的随机键来测试我对单词的了解。

这是我目前拥有的:

import random

witcher_dic =  {'bridles' : 'уздцы'  , 'hum' : 'гул' , 'to become deserted' : 'опустеть', 'linen' : 'полотяный' , 'apron' : 'фартук' ,
               'pockmarked (object)' : 'щербатый' , 'quiver (arrow)' : 'колчан' , 'to take a sip' : 'обхлебнуть' ,
               'to grunt' : 'буркнуть' , 'vile/foul' : 'паскудный' , 'pockmarked (person)' : 'рябой' , 'big-man' : 'верзила' ,
               'punk' : 'шпана' , 'to bark (person)' : 'гархнуть' , 'bastard, premature child' : 'недосонок' ,
               'to mumble' : 'промямлить' , 'to bark (person2)' : 'рявкнуть' , 'to look around oneself' : 'озираться' ,
               'oliquely' : 'наискось' , 'a mess/fuss' : 'кутерьма' , 'bolt (sound)' : 'грохот' , 'to blink' : 'шмяхнуться' ,
               'dissected' : 'рассеченный' , 'to wriggle' : 'извиваться', 'tender/sensitive' : 'чуткий' , 'to hang to' : 'облепить',
               'a clang/clash' : 'лязг' , 'to snuggle up to' : 'прильнуть' , 'boot-leg' : 'голенищ' , 'stuffing' : 'набивки' ,
               'cuffs' : 'манжеты' , 'to jump up' : 'вскочить' , 'to dart off' : 'помчаться' , 'to scream' : 'заволить' , 'shrilly' : 'пронзительно',
               'to back away' : 'пятиться' , 'loaded (horse)' : 'навьюченный'}


def ranWord():
    word = random.choice(list(witcher_dic.keys()))
    return word

while True:

    print(ranWord())
    guess = input('Please enter the translation for this word: ')
    if guess == witcher_dic[word]:
        print('Well done!')
    else:
        input(print('Please try again: '))

input('Press any key to exit')

对于格式和缩进表示抱歉,但我是 Whosebug 的新手,仍在学习中!

我想问题出在线上:if guess == witcher_dic[word]

程序应将用户条目与字典值相匹配。

以下是我看到的问题:

  1. 调用 ranWord 的结果没有保存在任何地方。稍后您会使用 word 但它不会被定义。你应该做 word = ranWord() 然后像 guess = input(f'Please enter the translation for {word}: ').
  2. 如果玩家猜对了,循环仍然继续。在打印 Well done!.
  3. 后添加一个 break 语句来终止循环
  4. Please try again 行似乎没有必要;当循环重新开始时,它会提示玩家进行新的猜测。删除 else,或将第二个 input 调用替换为 print(Your guess was incorrect.)

您的代码存在一些问题。

1.) 要使用非 ascii 字符,您需要使用 "magic" 注释
声明编码 2.) ranWord 只在它自己的范围内定义 word 所以你不能在函数外使用它。我建议学习 scope
3.) input(print(str)) 是无效语法。使用 input(str)
4.) 这不是真正的问题,但你永远不会退出 while 循环,所以你将永远翻译单词。您可以决定如何处理

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#include these!^

import random

witcher_dic =  {'bridles' : 'уздцы'  , 'hum' : 'гул' , 'to become deserted' : 'опустеть', 'linen' : 'полотяный' , 'apron' : 'фартук' ,
               'pockmarked (object)' : 'щербатый' , 'quiver (arrow)' : 'колчан' , 'to take a sip' : 'обхлебнуть' ,
               'to grunt' : 'буркнуть' , 'vile/foul' : 'паскудный' , 'pockmarked (person)' : 'рябой' , 'big-man' : 'верзила' ,
               'punk' : 'шпана' , 'to bark (person)' : 'гархнуть' , 'bastard, premature child' : 'недосонок' ,
               'to mumble' : 'промямлить' , 'to bark (person2)' : 'рявкнуть' , 'to look around oneself' : 'озираться' ,
               'oliquely' : 'наискось' , 'a mess/fuss' : 'кутерьма' , 'bolt (sound)' : 'грохот' , 'to blink' : 'шмяхнуться' ,
               'dissected' : 'рассеченный' , 'to wriggle' : 'извиваться', 'tender/sensitive' : 'чуткий' , 'to hang to' : 'облепить',
               'a clang/clash' : 'лязг' , 'to snuggle up to' : 'прильнуть' , 'boot-leg' : 'голенищ' , 'stuffing' : 'набивки' ,
               'cuffs' : 'манжеты' , 'to jump up' : 'вскочить' , 'to dart off' : 'помчаться' , 'to scream' : 'заволить' , 'shrilly' : 'пронзительно',
               'to back away' : 'пятиться' , 'loaded (horse)' : 'навьюченный'}


def ranWord():
    word = random.choice(list(witcher_dic.keys()))
    return word

while True:
    wrd = ranWord()
    print(wrd)
    guess = input('Please enter the translation for this word: ')
    if guess == witcher_dic[wrd]:
        print('Well done!') # maybe break here after a certain amount correct?
    else:
        input('Please try again: ') #or instead of trying again you can just lose when you answer incorrectly

input('Press any key to exit')