如何在不重复代码行的情况下询问列表中的所有问题?

时间:2016-08-18 21:54:50

标签: python python-3.x

question = ["1 – Which seven-a-side ball game is played in a swimming pool?",
"2 - When was the Olympics last held in London?",
        "3 - What is the world record time of the men's 100m sprint?",
        "4 - The latest Bond song was sung by whom?",
        "5 - Who won the Euro 2016 Final?",
        "6 - Who is the mascot of Pokemon?",
        "7 - How many stars are on the U.S flag?",
        "8 - If 1 = 5, 2 = 10, 3 = 15 and 4 = 20, what does 5 =?",
        "9 - In a right angled triangle one side is 3 and another side is 4, what is the length of the hypotenuse?",
        "10 - What is the 7th decimal place of pi?"]
multi1 = ["A: Marco Polo","A: 1944","A:9.58seconds","A: Charlie Puth","A: Portugal","A: Mew","A: 49","A: 25","A: 2","A: 4"]
multi2 = ["B: Polo","B: 2004","B: 9.68seconds","B: Sam Smith","B: Wales","B: Mewtwo","B: 52","B: 4","B: 5","B: 1"]
multi3 = ["C: Water Polo","C: 2008","C: 9.54seconds","C: Adele","C: France","C: Pikachu","C: 51","C: 5","C: 3.5","C: 9"]
multi4 = ["D: Polo Marco","D: 2012","D: 9.60seconds","D: Daniel Craig","D: Germany","D: Togepi","D: 50","D: 1","D: 6","D: 6"]
correctAnswer = ['C','D','A','B','A','C','D','D','B','D']
valueWon = ['£0','£100','£2500','£500','£1000','£2500','£5000','£10000','£100000','£1000000']
x = input(question[0] + ' ' +multi1[0]+ ' ' +multi2[0]+ ' ' +multi3[0]+ ' ' +multi4[0])
if x == ("A","B","C"):
    print("I'm sorry that was incorrect,",correctAnswer[0],"was the correct answer, you won,",valueWon[0])
else:
    y = input("Congratulations, you won" +" " +valueWon[1]+" " +"would you like to continue, yes or no?")
if y == ("No","no"):
    exit

我正在制作一个'谁想要成为百万富翁'的游戏,我想问一下列表中的所有问题,而不重复我上面使用的所有代码,因为它太冗长而且我知道有一种更简单的方法。感谢

3 个答案:

答案 0 :(得分:2)

一种方法是将您的问题转化为单独的Class对象。

class Question():
    def __init__(self, id, question, answers, correct_answer):
        self.id = id
        self.question = question
        self.answers = answers
        self.correct_answer = correct_answer


question_one = Question(
    1,
    "Which seven-a-side ball game is played in a swimming pool?",
    {"1":"Marco Polo", "2":"Water Polo", "3":"Polo", "4":"Polo Marco"},
    "Water Polo"
                       )

question_list = [question_one]
for _ in question_list:
    print("Question number {0}: {1}".format(_.id, _.question))
    answer = input("{0}\n".format(_.answers))
    if _.answers[answer] == _.correct_answer:
        print("You're correct!")

结果输出为:

>>>Which seven-a-side ball game is played in a swimming pool?
>>>{'2': 'Water Polo', '1': 'Marco Polo', '3': 'Polo', '4': 'Polo Marco'}
>>>2
>>>You're correct!

依此类推。请注意,如果您使用的是Python 2.7,则需要从answers词典中删除引号。

答案 1 :(得分:0)

我假设这就是你想要的。我看到很多可以对代码进行的即兴创作。但是,对于初学者,我仍然在这里提出我的答案。

question = ["1 – Which seven-a-side ball game is played in a swimming pool?","2 - When was the Olympics last held in London?",
    "3 - What is the world record time of the men's 100m sprint?",
    "4 - The latest Bond song was sung by whom?",
    "5 - Who won the Euro 2016 Final?",
    "6 - Who is the mascot of Pokemon?",
    "7 - How many stars are on the U.S flag?",
    "8 - If 1 = 5, 2 = 10, 3 = 15 and 4 = 20, what does 5 =?",
    "9 - In a right angled triangle one side is 3 and another side is 4,what is the length of the hypotenuse?",
    "10 - What is the 7th decimal place of pi?"]
multi1 = ["A: Marco Polo","A: 1944","A:9.58seconds","A: Charlie Puth","A:Portugal","A: Mew","A: 49","A: 25","A: 2","A: 4"]
multi2 = ["B: Polo","B: 2004","B: 9.68seconds","B: Sam Smith","B: Wales","B: Mewtwo","B: 52","B: 4","B: 5","B: 1"]
multi3 = ["C: Water Polo","C: 2008","C: 9.54seconds","C: Adele","C: France","C: Pikachu","C: 51","C: 5","C: 3.5","C: 9"]
multi4 = ["D: Polo Marco","D: 2012","D: 9.60seconds","D: Daniel Craig","D: Germany","D: Togepi","D: 50","D: 1","D: 6","D: 6"]
correctAnswer = ['C','D','A','B','A','C','D','D','B','D']
valueWon = ['£0','£100','£2500','£500','£1000','£2500','£5000','£10000','£100000','£1000000']

for i,j in enumerate(question):
    x = input(j + '\n ' +multi1[i]+ '\n ' +multi2[i]+ '\n ' +multi3[i]+ '\n ' +multi4[i]+'\n')
    if x == ("A","B","C"):
            print("I'm sorry that was incorrect,",correctAnswer[i],"was the correct answer, you won,",valueWon[i])
    else:
            y = input("Congratulations, you won" +" " +valueWon[i]+" " +"would you like to continue, yes or no?")
            if y == ("No","no"):
                    break

我希望这会有所帮助。

谢谢!

答案 2 :(得分:0)

简单地回答您的问题:您可以使用foreach循环遍历问题列表中的每个问题。

for q in question:
    #do something with q, the for loop will do this for every q in your list 'question'

问题在于您无法访问相应的多项选择答案,正确答案或轻松获得的价值。也就是说,如果循环是迭代的并且您在第二个问题上,那么for循环如何知道第二个多项选择答案,第二个正确答案等?

你可以这样做:

for q in question:
    #set idx equal to the index number of the current question,
    #so if you're on question 3, it'll be index 2, and you can use
    #idx to grab the corresponding multiple choice answers/correct answer/etc
    idx = question.index(q)

    #have the user input an answer
    x = input(q + ' ' + multi1[idx] + ... + multi4[idx])
    if (x != correctAnswer[idx]):
        print("I'm sorry that was incorrect,",correctAnswer[idx],"was the correct answer")
    else:
        y = input("Congratulations, you won" +" " +valueWon[idx]+" " +"would you like to continue, yes or no?")
        if y == ("No","no"):
            break

但是enumerate已经为你做了这个:

for i, q in enumerate(question):
    #have the user input an answer
    x = input(q + ' ' + multi1[i] + ... + multi4[i])
    if (x != correctAnswer[i]):
        print("I'm sorry that was incorrect,",correctAnswer[i],"was the correct answer")
    else:
        y = input("Congratulations, you won" +" " +valueWon[i]+" " +"would you like to continue, yes or no?")
        if y == ("No","no"):
            break

您可以(也可能应该)为此创建一个类,这样您的所有数据都会耦合在一起,而不是分散在多个对象上。然后你只需要迭代对象列表并以相同的方式使用它们。