当词典在lis中时打印词典的值

2024-05-01 21:48:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个小问题,在我的代码,我会很高兴,如果你能帮我解决它

我的问题是当词典在列表中时打印词典的值。 如果你能帮我解决代码中的这个问题,我会很高兴的

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()

谢谢大家


Tags: thetoanswerinyouforinputsize
1条回答
网友
1楼 · 发布于 2024-05-01 21:48:07

试试这个。这只是一个补丁。不是真正的Python

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

相关问题 更多 >