Python中的多项选择测验:如何为字典random.sample输出分配数字标签

时间:2018-04-24 20:56:04

标签: python-3.x

Qlib是一个问题词典:这样的答案对:

Qlib = dict()
Qlib["Apple"]="green"
Qlib["Orange"]="orange"
Qlib["Banana"]="yellow"
Qlib["Strawberry"]="red"

问题随机播放的功能:

def Qshuffle(self):
    for x in range(1):
        choices = random.sample(list(Qlib),4)
        random_fruit = random.choice(choices)
        main_typed = "Which fruit is the following colour...??\n\n\n"

        print('{0} {1}\n\n'.format(main_typed, random_fruit.center(85)))
        print('Choices:\n' + "\n".join([Qlib[x] for x in choices]))
    Options = ['Option [a]','Option [b]','Option [c]','Option [d]']
    for op in Options:
        print (op.rjust(38))
    answer = input("Enter answer a,b,c, or d")

有人能指出我如何将(a,b,c或d)分配给多项选项并让字母实际上与random.sample选项相对应的方向吗?目前我可以用多选答案呈现随机问题,但我不知道如何在多个选项中分配字母(a,b,c,d)或(1,2,3,4)它们的创建是为了让用户只需输入一个'或者' b'选择答案并将其分配给特定选择时?

2 个答案:

答案 0 :(得分:1)

您的问题有点难以理解,但我猜您试图检查用户的答案是否与随机选择的值相同。如果是这种情况,那么这样做:

Qlib = dict()
Qlib["Apple"]="green"
Qlib["Plum"]="purple"
Qlib["Banana"]="yellow"
Qlib["Strawberry"]="red"

def qshuffle():
    choices = random.sample(list(Qlib),4)
    random_fruit = random.choice(choices)
    print("Which fruit is of {0} colour??\n".format(Qlib[random_fruit]))
    print('--- Choices: ---\n' + "\n".join([x for x in Qlib]))
    answer = input("You answer is >> ")

    if answer.capitalize() == random_fruit:
        print("That's right")
    else:
        print("Sorry, wrong answer.")

答案 1 :(得分:1)

您可以存储选项列表,然后根据用户输入的索引获取颜色。然后从字典中获取颜色并进行比较。

以下是工作版本:

import random

Qlib = dict()
Qlib["Apple"]="green"
Qlib["Orange"]="orange"
Qlib["Banana"]="yellow"
Qlib["Strawberry"]="red"

#defining options for further usage, optional but required in this code
letters = ["a","b","c","d"]

#I'm not sure about self in it
def Qshuffle(self):
    #I'm not sure this for x in range(1) is necessary
    for x in range(1):
        choices = random.sample(list(Qlib),4)
        random_fruit = random.choice(choices)
        main_typed = "Which fruit is the following colour...??\n\n\n"

        print('{0} {1}\n\n'.format(main_typed, random_fruit.center(85)))
        print('Choices:')
    #Defining/storing list of choices for further use, necessary
    options = list(Qlib[x] for x in choices)
    #some improvements of printing choices
    for i in range(len(options)):
        op = str(options[i])+" ["+str(letters[i])+"]"
        print(op.rjust(38))
    answer = input("Enter answer a,b,c, or d\n>>> ")

    if answer.lower() in letters:
        ########
        #Some debug information, can be deleted
        print("="*5)
        print(Qlib[random_fruit])                       #the color of random fruit
        print(letters.index(answer.lower()))            #index of selected answer
        print(options)                                  #list of options
        print(options[letters.index(answer.lower())])   #the color that user selected
        print("="*5)
        ########
        #checking that user chose the color of the random fruit
        if Qlib[random_fruit] == options[letters.index(answer.lower())]:
            print("correct")
        else:
            print("wrong")
    else:
        print("incorrect input")

Qshuffle("")