将键返回到随机选择的字典值

时间:2019-09-07 20:08:36

标签: python-3.x

有人要求我构建一个带有提供键和值的字典的抽认卡程序。想法是选择一个字母以查看键(单词)或值(单词的定义),然后在按Enter键时将值提供给键,反之亦然。

我可以随机生成一个密钥并在按Enter键时显示值...

我可以随机生成一个值,但是在输入时很难显示密钥

#import the random module
from random import *

import csv
file = open('dictionary.txt', 'r')

#define a function for file_to_dictionary
def file_to_dictionary(filename):
    """return a dictionary with the contents of a file"""
    file = open(filename, 'r')
    reader = csv.reader(file)
    dictionary = {}
    for row in reader:
        dictionary[row[0]] = row[1]
    return dictionary

#set up the glossary
glossary = file_to_dictionary('dictionary.txt')
glossary.values()

#define a function to show a random flashcard
def show_flashcard(): #this is the function header
    """show the user a random key and ask them to define it.
    Show the definition when the user presses return""" # the docstring
    random_key = choice(list(glossary)) #obtain a random key by returning the glossary into a list of keys with list() and choice() for random
    print('Define: ', random_key)
    input ('Press return to see the definition')
    print (glossary[random_key])

#define a function to show a random definition
def show_definition():
    """show a random value from the dictionary and ask them to name the word"""
    random_value = choice(list(glossary.values()))
    print ('What word relates to this definition ?', random_value)
    input ('Press return to see the word')
    print (glossary.values[random_value])

当我为一个随机键运行代码,然后按Enter键以查看值(定义)时,以及当我为一个随机值运行代码时,我都得到了所需的结果,但随后却难以返回匹配的键值。

下面是正确运行并返回错误的代码输出:

Press s to see a flashcard, Press d to see definition, or press q to quit s
Define:  container
Press return to see the definition
(As applied to computing) a cloud technology where the virtualisation happens within the operating system itself; a container is a portable module in which software can be run.
Press s to see a flashcard, Press d to see definition, or press q to quit d
What word relates to this definition ? Legible data unprotected by encryption.
Press return to see the word
Traceback (most recent call last):
  File "/Users/degsy/Desktop/Python/tet_gloss.py", line 48, in <module>
    show_definition()
  File "/Users/degsy/Desktop/Python/tet_gloss.py", line 37, in show_definition
    print (glossary.values[random_value])
TypeError: 'builtin_function_or_method' object is not subscriptable

1 个答案:

答案 0 :(得分:0)

多个键可以具有相同的值。从{1:1, 2:1, 3:1, 4:1}的值1中获取密钥会为值1提供4种可能的密钥。

最简单的方法可能是在询问时直接取一个随机的key,value tuple 并询问:

from random import choice
def show_definition(d):
    """show a random value from the dictionary and ask them to name the word"""
    # get a random key,value from .items()
    random_key, random_value = choice(list(d.items()))
    print ('What word relates to this definition ?', random_value)
    input ('Press return to see the word')
    print (random_key)

show_definition(  {1:"one", 2:"two", 3:"three"} )
show_definition(  {1:"one", 2:"two", 3:"three"} )
show_definition(  {1:"one", 2:"two", 3:"three"} )

输出:

What word relates to this definition ? three
Press return to see the word
3
What word relates to this definition ? one
Press return to see the word
1
What word relates to this definition ? two
Press return to see the word
2

请参见dict.items()