有没有办法在Python字典中附加第3个值? [英] Is there a way to attach a 3rd value in Python dictionaries?

查看:276
本文介绍了有没有办法在Python字典中附加第3个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个愚蠢的问题,我相信我很有可能会采取完全错误的方法来解决我的问题,因此,如果有人知道更好的方法,请随时纠正我/将我指向正确的位置方向.

This may be a stupid question, and I believe there is a good chance I am taking the completely wrong approach to solving my problem, so if anyone knows a better way, please feel free to correct me / point me in the right direction.

我正在尝试用Python编写一个基本的测验程序,但我似乎找不到一种很好的方法来实际用问题的答案来称呼问题并存储正确的答案.理想情况下,我也希望将问题中的答案也打乱一下,例如,#1的答案也不一定总是"A",但是此步距现在太远了.

I'm trying to make a basic quiz program in Python for fun and I can't seem to find a good way to actually both call the questions with their answers AND store what the correct answer should be. Ideally I'd like to shuffle the answers within the question around as well so for example the answer to #1 is not always "A" as well, but that's going a step too far at the moment.

无论如何,我认为字典可以工作.我会做这样的事情:

Anyway, I figured a dictionary could work. I'd do something like this:

test = {
    '1':    "What is 1+1?\nA) 2\nB) 11\nC) 1\nD) None of the above.\n\n", #A
    '2':    "What is 2+2?\nA) 2\nB) 4\nC) 1\nD) None of the above.\n\n", #B
    '3':    "What is 3+3?\nA) 2\nB) 11\nC) 6\nD) None of the above.\n\n", #C
    '4':    "What is 4+4?\nA) 2\nB) 11\nC) 1\nD) None of the above.\n\n", #D
    '5':    "What is 5+5?\nA) 2\nB) 11\nC) 10\nD) None of the above.\n\n", #C
    '6':    "What is 6+6?\nA) 2\nB) 12\nC) 1\nD) None of the above.\n\n", #B
    }

answer = raw_input(test['1'])

我可以使用它轻松地按顺序或随机进行一些修改即可打印出问题.但是,我没有办法在字典上附加正确的答案,这意味着我必须做一些使if语句变得可笑的事情.有人知道解决方案吗?有没有一种方法可以向每个字典条目添加第三个值?我完全忽略了变通办法吗?任何帮助将不胜感激.

Which I could use to easily print out questions either in order or randomly with a little modification. However, I have no way of actually attaching the correct answer to the dictionary which means I'd have to do something like make ridiculous amounts of if statements. Does anyone know a solution? Is there a way to add a 3rd value to each dictionary entry? A work-around I'm completely overlooking? Any help would be greatly appreciated.

推荐答案

您似乎在错误地布置数据结构.不应像其他答案所建议的那样使用元组或列表的字典,而应该使用字典列表.

It looks like you're laying out your data structure wrong. Instead of a dictionary of tuples or lists as your other answers are suggesting, you SHOULD be using a list of dictionaries.

questions = [
    {'question':"What is 1+1?\nA) 2\nB) 11\nC) 1\nD) None of the above.\n\n",
     'answer':"#A"},
    {'question':"What is 2+2?\nA) 2\nB) 4\nC) 1\nD) None of the above.\n\n",
     'answer':"#B"},
    ...}

这将使您遍历以下问题:

This will let you iterate through your questions with:

for q in questions:
    print(q['question'])
    ans = input(">> ")
    if ans == q['answer']:
        # correct!
    else:
        # wrong!

如果您仍然需要数字,则可以将number保存为字典的另一个键(在数据库中进行类似的行操作,以number作为主键):

If you still need numbers, you could either save number as another key of the dictionary (making this kind of like rows in a database, with number being the primary key):

{'number': 1,
 'question': ...,
 'answer': ...}

或者只是使用enumeratestart关键字参数来遍历您的问题

Or just iterate through your questions using enumerate with the start keyword argument

for q_num, q in enumerate(questions, start=1):
    print("{}. {}".format(q_num, q['question']))
    ...

这篇关于有没有办法在Python字典中附加第3个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆