如何在Python中找到两个单独单词的属性?

时间:2011-11-18 19:03:54

标签: python random

我需要随机选择两个不同的单词,然后获取与它们相关联的值。让我进一步解释一下:

第一个列表为"masculine, feminine, neuter, plural",第二个列表为"nom, acc, gen, dat"

我希望程序随机选择一个,然后让用户提供选项。因此,如果它选择了masc + nom,则用户应该回答(通过input选项)。当他们这样做时,我想告诉他们他们是对还是错。

我希望这是有道理的。

你会如何实现这个?我可以随意选择单词,但我不确定如何找到答案。

任何帮助都会很棒。谢谢!

4 个答案:

答案 0 :(得分:1)

听起来你需要某种正确答案的

如果你制作这样的表:

[["der","die","das","die"],
 ["den","die","das","die"],
 ["dem","der","dem","den"],
 ["des","der","des","der"]]

您可以按索引查看条目,如下所示:

mfnp = "masculine, feminine, neuter, plural".split(',')
nadg = "nom, acc, gen, dat".split(',')

s1 = "masculine"
s2 = "acc"
print table[mnfp.index(s1)][nadg.index(s2)]

使用字典:

或者,您可以将所有条目放在字典中:

d = {}
d["masculine"]["nom"] = "der"
d["masculine"]["acc"] = "den"
#...
d["neuter"]["gen"] = "dem"
#etcetera

答案 1 :(得分:1)

# list labels for columns
columns = ['Masculine', 'Neuter', 'Feminine', 'Plural']

# list labels for rows
rows = ['Nominative', 'Accusative', 'Dative', 'Genitive']

# create a grid of answers for combination of column-rows
answers = [['der', 'das', 'die', 'die'],
        ['den', 'das', 'die', 'die'],
        ['dem', 'dem', 'der', 'den'],
        ['des', 'des', 'der', 'der']]

def correct_answer(column_name, row_name):
    # get correct indexes
    column_index = columns.index(column_name)
    row_index = rows.index(row_name)
    # used indexes to get answer
    return answers[row_index][column_index]

print correct_answer('Masculine', 'Accusative')

输出:

den

答案 2 :(得分:0)

你想要一个单词及其属性的列表,这些是你的答案,你反对:

list1 = ["masc", "fem"]
list2 = ["dom", "gen"]
answers = {"word1": ["masc", "nom"], "word2": ["fem", "gen"], ...}

prop1 = random.choice(list1)
prop2 = random.choice(list2)
answer = input_from_user(prop1, prop2)
if prop1 in answers[answer] and prop2 in answers[answer]:
    print "correct"
else:
    print "incorrect"

答案 3 :(得分:0)

这会从你的列表中随机抽出一个字符串

from random import *

list1 = ['masculine', 'feminine', 'neuter','plural']
list2 = ['nom','acc','gen','dat']

var1 = random.sample(list1,1)
var2 = random.sample(list2,1)

var3 = str(var1) + '+' + str(var2)