首页 > 解决方案 > 将键返回到随机选择的字典值

问题描述

我被要求使用提供键和值的字典构建一个抽认卡程序。这个想法是选择一个字母来查看键(单词)或值(单词的定义),然后当您按下回车键时,将值提供给键,反之亦然。

我可以随机生成一个键并在按 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])

当我运行随机键的代码然后按回车键查看值(定义)以及运行随机值的代码时,我得到了所需的结果,但随后努力返回键以匹配值。

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

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

标签: python-3.x

解决方案


您可以为多个键设置相同的值。从值 1 中获取键为值 1 {1:1, 2:1, 3:1, 4:1}提供 4 个可能的键。

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

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()


推荐阅读