当字典在列表中时打印字典的值

时间:2015-03-27 10:01:36

标签: python list dictionary

我的代码中有一点问题,如果你能帮我解决,我会很高兴。

我的问题是当字典在列表中时打印字典的值。 如果你能帮助我在我的代码中解决这个问题,我很乐意。

def func0():

print "Quiz Program"
print "----------------------------------------------"
print "This program stores quiz questions and allows you to take the quiz"
print "----------------------------------------------"
size = input("Enter number of questions: ")
i = 0
for i in xrange(size):
    question = raw_input("Please enter a question, enter to exit: ")
    question_dictionary =  {"question" : question }
    count = input("How many possible answers  do you want this question to have: ")
    answer_list = []
    for j in xrange(count):
        answer = raw_input("Please enter the next answer: ")
        answer_list.append(answer)
    print "------------------------------------------------"
    for j in xrange(count):
        print j + 1,"." ,answer_list[j]
    print "------------------------------------------------"
    question_dictionary["answers"] = answer_list
    correct_answer = input("Please enter the value of the correct answer: ")
    question_dictionary["correct_answer"] = correct_answer
    new_list = []
    new_list.append(question_dictionary)
print "------------------------------------------------"
true_answer = 0
for i in xrange(size):
    print "question" ,i,".",  # problem to print the question
    print "------------------------------------------------"
    print "Possible Answers: "
    for j in xrange(count):
        print j + 1,"." ,answer_list[j]
    print "------------------------------------------------"
    user_answer = input("Please select an answer: ")
    if(user_answer == ): # problem to get the right answer
        print "well done, you answered the question correctly!"
        true_answer += 1
    else:
        print "sorry, you didn't get the correct answer."
    print "------------------------------------------------"
print "Your final score was", true_answer," out of", size

def main():

func0()

if __name__ == "__main__":
     main()

谢谢大家

1 个答案:

答案 0 :(得分:0)

试试这个。这只是一个修复。没有实际的pythonic。

print "Quiz Program"
print "----------------------------------------------"
print "This program stores quiz questions and allows you to take the quiz"
print "----------------------------------------------"
size = input("Enter number of questions: ")
i = 0
new_list = []
for i in xrange(size):
    question = raw_input("Please enter a question, enter to exit: ")
    question_dictionary =  {"question" : question }
    count = input("How many possible answers  do you want this question to have: ")
    answer_list = []
    for j in xrange(count):
        answer = raw_input("Please enter the next answer: ")
        answer_list.append(answer)
    print "------------------------------------------------"
    for j in xrange(count):
        print j + 1,"." ,answer_list[j]
    print "------------------------------------------------"
    question_dictionary["answers"] = answer_list
    correct_answer = input("Please enter the value of the correct answer: ")
    question_dictionary["correct_answer"] = correct_answer
    new_list.append(question_dictionary)
print "------------------------------------------------"
print new_list
true_answer = 0
for i in xrange(size):
    print "question" ,new_list[i]['question'],".\n",  # problem to print the question
    print "------------------------------------------------"
    print "Possible Answers: "
    for j in xrange(len(new_list[i]['answers'])):
        print j + 1,"." ,new_list[i]['answers'][j]
    print "------------------------------------------------"
    user_answer = input("Please select an answer: ")
    if(user_answer == new_list[i]['correct_answer']): # problem to get the right answer
        print "well done, you answered the question correctly!"
        true_answer += 1
    else:
        print "sorry, you didn't get the correct answer."
    print "------------------------------------------------"
print "Your final score was", true_answer," out of", size