如何制作一个可以用Python存储用户输入的字典?

如何制作一个可以用Python存储用户输入的字典?,python,python-2.7,Python,Python 2.7,我想知道我将如何能够使一本字典,可以存储多个用户输入和打印出来,如果要求。我用这个代码尝试过,但我想我错了,因为它不起作用: def new_questions(): new_question = {} while new_question: newly_added_question = raw_input('Please type the question:') newly_added_question_ans1 = raw_input('Please type the COR

我想知道我将如何能够使一本字典,可以存储多个用户输入和打印出来,如果要求。我用这个代码尝试过,但我想我错了,因为它不起作用:

def new_questions():
new_question = {}
while new_question:
    newly_added_question = raw_input('Please type the question:')
    newly_added_question_ans1 = raw_input('Please type the CORRECT answer:')
    newly_added_question_ans2 = raw_input('Please type an incorrect answer:')
    newly_added_question_ans3 = raw_input('Please type an incorrect answer:')
    new_question[newly_added_question] = newly_added_question_ans1
    new_question[newly_added_question] = newly_added_question_ans2
    new_question[newly_added_question] = newly_added_question_ans3

您可以尝试列出所有答案并将其存储为:

new_question[newly_added_question] = [newly_added_question_ans1, newly_added_question_ans2, newly_added_question_ans3]

我建议使用字典存储
新问题
字典的每个值,如下所示:

from pprint import pprint    

def new_questions():
    new_question = True

    questions = {}
    while new_question:
        question = raw_input('Please type the question:')
        ans1 = raw_input('Please type the CORRECT answer:')
        ans2 = raw_input('Please type an incorrect answer:')
        ans3 = raw_input('Please type an incorrect answer:')

        answers = {
            'correct': ans1,
            'incorrect_1': ans2,
            'incorrect_2': ans3
        }

        questions[question] = answers

        next_question = raw_input('\nAnother question? (Y/N): ')

        if next_question.lower() == 'n':
            new_question = False

    pprint(questions)

new_questions()

样本输出:

Please type the question:How many e's in eternity?
Please type the CORRECT answer:2
Please type an incorrect answer:3
Please type an incorrect answer:42

Another question? (Y/N): y
Please type the question:Is this real?
Please type the CORRECT answer:Yes.
Please type an incorrect answer:No.
Please type an incorrect answer:Maybe?
Another question? (Y/N): n
{"How many e's in eternity?": {'correct': '2',
                               'incorrect_1': '3',
                               'incorrect_2': '42'},
 'Is this real?': {'correct': 'Yes.',
                   'incorrect_1': 'No.',
                   'incorrect_2': 'Maybe?'}}

您可以通过以下方式访问每个问题的答案:
questions[question]['correct']
questions[question]['correct\u 1']]
,等等。

从您的代码中,每当您为问题指定新答案时,您似乎正在覆盖字典中键的值

尝试使用一些容器来存储密钥的值。i、 e要在字典中保存问题的答案,可以将列表用作容器。见下面的例子

result = {}  #Empty dictionary

def new_question(result):
  question = input("Enter the question: ")
  ans1 = input("Enter the Correct answer ")
  ans2 = input("Enter the Incorrect answer ")
  ans3 = input("Enter the another Incorrect answer ")
  answers = [ans1,ans2,ans3]  #storing answers in a list
  result[question] = answers  #setting answers to question

new_question(result) #add questions and answers by calling this fuction, call this in a loop if multiple questions
print(result) # print all the questions and answers

注意:如果您已经定义了答案的选项数量,那么您也可以为用户输入添加一个循环。

一个字典只能为每个键存储一个值(尽管该值可以是一个容器,如列表或另一个字典);您正在覆盖以前的条目。还请注意,您应该提供一个示例来说明“不工作”的实际含义。您必须存储字符串的集合,例如列表或元组。