一个?在元组中有多个答案

时间:2014-06-09 01:09:36

标签: python

我正在学习Python。我在练习时试图进行测验。我有不同的问题,有些有几个答案。我如何在if语句中调用所有答案。 (并非所有问题都有相同数量的答案)

def english():

        name = raw_input("What's your name? ")

        print "Hi, Welcome %s to our english review session." % name

        anglais = [
                ('If I know, I ........ you. (to tell)', 'will tell'),
                ('If I knew, I ........ you. (to tell)', 'would tell'),
                ('If I had known, I ........ you. (to tell)', 'would have told'),
                ('Had I known, I ........ you. (to tell)', 'would have told'),
                ('If I ........ money, I\'d have baught you a phone. (to have)', 'had had'),
                #('Balla Gaye would\'ve gained if B52 ........ (to )' , ''),
                ('If Amet ....... playing You\'ll play. (to finish)', 'finishes'),
                #('Had I had a good mark my father ......... ', ''),
                ('If Balla Gaye worked hard, he ...... the match. (to lose/not).', 'wouldn\'t lose'),
                ('If Zeyna were a hard worker, She ........ (to succeed).', 'would succeed', '\'d succeed'),
                ('If mom was here, you ........ me. (to bit/not)', 'would not bite', 'wouldn\'t bite'),
                ('If you asked me, I ........ me. (to answer)', 'would answer','\'d answer')


        ]

        number_of_right_answers = 0
        shuffle(anglais)

        for question, answer, answer_1, answer_2 in anglais:
                right_answer = raw_input(question)
                if right_answer == answer or right_answer == answer_1 or right_answer == answer_2:
                        print "Bonne reponse."
                        number_of_right_answers += 1
                else:
                        print "Mauvaise reponse. La bonne reponse est " + answer

        print "Vous avez trouve %d de question sur %d" % (number_of_right_answers, len(anglais))     

start()

1 个答案:

答案 0 :(得分:2)

不是尝试解压缩元组,而是将其切片并使用循环或any和生成器表达式来检查每个可能的答案:

for item in anglais:
    question, answers = item[0], item[1:]
    user_answer = raw_input(question)
    if any(user_answer == valid_answer for valid_answer in answers):
        ...