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

Python 如何在不重复代码行的情况下询问列表中的所有问题?,python,python-3.x,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?",

我正在做一个“谁想成为百万富翁”的游戏,我想问列表中的所有问题,但不要重复我上面使用的所有代码,因为它太长了,我知道有一个更简单的方法。谢谢

我想这就是你想要的。我看到很多可以对代码进行的即兴创作。不过,首先,我还是在这里给出我的答案

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
我希望这有帮助


谢谢

我想这就是你想要的。我看到很多可以对代码进行的即兴创作。不过,首先,我还是在这里给出我的答案

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
我希望这有帮助


谢谢

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

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
结果将是:

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!")

等等等等。请注意,如果您使用的是Python 2.7,则需要删除
答案中数字的引号。

一种方法是将问题转换为单个类对象

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
结果将是:

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!")

等等等等。请注意,如果您使用的是Python 2.7,则需要删除
答案
目录中数字的引号。

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

>>>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!
问题在于,您无法访问相应的多项选择答案、正确答案或轻松获得的值。也就是说,如果循环是迭代的,而您在第二个问题上,for循环如何知道如何呈现第二个选择题答案、第二个正确答案等

你可以做:

for q in question:
    #do something with q, the for loop will do this for every q in your list 'question'
但是,
enumerate
已经为您执行了以下操作:

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

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

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

>>>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!
问题在于,您无法访问相应的多项选择答案、正确答案或轻松获得的值。也就是说,如果循环是迭代的,而您在第二个问题上,for循环如何知道如何呈现第二个选择题答案、第二个正确答案等

你可以做:

for q in question:
    #do something with q, the for loop will do this for every q in your list 'question'
但是,
enumerate
已经为您执行了以下操作:

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

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

查找“while-loop”。查找“while-loop”。在枚举(问题)
中,您是否需要为i,q设置
看起来像一个简单的打字错误有趣的是,当我纠正您的错误时,我最初为q,i设置了
。也忘了列举。谢谢你,我没有考虑使用enumerate,但是它很有用,谢谢你认为enumerate(问题)
中的i,q需要
看起来像一个简单的打字错误有趣的是,当我去纠正你的时候,我原来有
for q,i
。也忘了列举。我必须使用它。谢谢这个,我没有考虑使用enumerate,但它有帮助,谢谢谢谢这个帮助,是的,它肯定有一些可以预防的漏洞。谢谢这个帮助,是的,它肯定有一些可以预防的漏洞。我不想把它们变成类对象,这肯定有帮助,谢谢,没问题!如果它有很大的帮助,请考虑检查绿色箭头:没有想到把它们变成类对象,这肯定是有帮助的,谢谢!如果它有很大的帮助,请考虑检查绿色箭头:P