更新多个列表索引时在python中进行For循环?

更新多个列表索引时在python中进行For循环?,python,list,for-loop,indexing,Python,List,For Loop,Indexing,我对python有点陌生。我已经完成了15个问题的测试程序,从15个问题中随机抽取5个,并将问题逐一打印给用户。每个问题有4个可能的答案 该程序运行良好,但我不想为每个问题编写代码块,而是想循环所有5个随机问题,并使其与每个问题的每个单独代码块做相同的事情。我已经习惯了Java,它看起来不太一样,所以我评论了for循环的一次糟糕尝试 我只是想让它更有效率,所以任何提示或指导都是有帮助的。我不要求对我的问题给出完整的答案。谢谢 #Print statement displaying a welco

我对python有点陌生。我已经完成了15个问题的测试程序,从15个问题中随机抽取5个,并将问题逐一打印给用户。每个问题有4个可能的答案

该程序运行良好,但我不想为每个问题编写代码块,而是想循环所有5个随机问题,并使其与每个问题的每个单独代码块做相同的事情。我已经习惯了Java,它看起来不太一样,所以我评论了for循环的一次糟糕尝试

我只是想让它更有效率,所以任何提示或指导都是有帮助的。我不要求对我的问题给出完整的答案。谢谢

#Print statement displaying a welcome message
print("***WELCOME TO THE RANDOM QUIZ***\n")

#Importing random library to use the random module
import random

#Creates a list with all 15 questions
questions = ['What is the capital city of Ireland?', 'What is the capital city of France?',
            'What is the capital city of Germany?', 'What is the capital city of Romania?',
            'What is the capital city of Russia?', 'What is the capital city of Belgium?',
            'What is the capital city of Switzerland?','What is the capital city of Egypt?',
            'What is the capital city of Japan?','What is the capital city of China?',
            'What is the capital city of Thailand?','What is the capital city of Brazil?',
            'What is the capital city of Argentina?','What is the capital city of Bolivia?',
            'What is the capital city of Mexico?']

#Creates a list with the answers for each of the 15 individual questions
possible_answers = ['a) Portlaoise | b) Waterford | c) Dublin | d) Galway ', 'a) Paris | b) Lyon | c) Nantes | d) Tolouse ',
                    'a) Frankfurt | b) Munich | c) Berlin | d) Dusseldorf ', 'a) Bucharest | b) Constanta | c) Galati | d) Sibiu ',
                    'a) Moscow | b) St.Petersburg | c) Novosirbirsk | d) Sochi ', 'a) Ghent | b) Bruges | c) Brussels | d) Antwerp ',
                    'a) Zurich | b) Geneva | c) Basel | d) Bern ', 'a) Cairo | b) Alexandria | c) Luxor | d) Giza ',
                    'a) Osaka | b) Yokohama | c) Nagoya | d) Tokyo ', 'a) Beijing | b) Shenzhen | c) Shanghai | d) Fuangzhou ',
                    'a) Bangkok | b) Chiang Mai | c) Pattaya | d) Hat Yai ', 'a) Rio de Janeiro | b) Sao Paolo | c) Salvador | d) Brasilia ',
                    'a) Mendoza | b) Rosario | c) Buenos Aires | d) Cordoba ', 'a) Santa Cruz de la Sierra | b) La Paz | c) El Alto | d) Oruro ',
                    'a) Mexico City | b) Guadalajara | c) Tijuana | d) Merida ']

#Creates a list with the right answers for each of the 15 individual questions
answers = ["Dublin","Paris","Berlin","Bucharest","Moscow","Brussels","Bern","Cairo","Tokyo","Beijing","Bangkok",
            "Brasilia","Buenos Aires","La Paz","Mexico City"]

#Creates variable and assigns the value of 5 which represents the number of random questions we need out of the possible 15
num_of_rand_q = 5

#This variable is created to keep track of the number of right answers by the user
right_q_count = 0

#Assign randomly selected question to a variable
list_of_rand_q = random.sample(questions, num_of_rand_q)


"""
In this operation we match the randomly selected question with the same question in the 'questions' list
to get its original index and assign the index value to a variable. This will help us later on to get the
answer of the exact question by using its original index value as indexes of each question and respective
answer match in both 'questions' and 'answers' lists. We do this for all 5 randomly selected questions.
"""
q_one_index = questions.index(list_of_rand_q[0])
q_two_index = questions.index(list_of_rand_q[1])
q_three_index = questions.index(list_of_rand_q[2])
q_four_index = questions.index(list_of_rand_q[3])
q_five_index = questions.index(list_of_rand_q[4])

"""
#Creating list of indexes so we can loop through them 
q_indexes = [q_one_index, q_two_index, q_three_index, q_four_index, q_five_index]

#Attempted for loop
i = 0
for i in list_of_rand_q[i]:
    print(list_of_rand_q[i])
    print(possible_answers[questions.index(list_of_rand_q[i])], "\n")
    answer_1 = input("Type the city name of which you think it's the capital! \n")

    if answer_1 in answers:
        print('Well done! This is the right answer!')
        right_q_count += 1
    else:
        print('It is the wrong answer...The right answer is actually : ', answers[q_indexes[i]])
"""


#First Question and same as other 4
#Print statement to ask the random question
print(list_of_rand_q[0])
#Print statement to display all 4 possible answers respective to the question
print(possible_answers[questions.index(list_of_rand_q[0])], "\n")
#We ask the user to type the capital city and store the user's input in the answer variable
answer = input("Type the city name of which you think it's the capital! \n")

#If statement checks if user's input matches the value in the answers list
if answer in answers:
    #If statement is true prints a message
    print('Well done! This is the right answer!\n')
    #right_q_count variable stores the number of right answers and we increment it everytime the answer given is right
    right_q_count += 1
#If the if statement is not true and user gets wrong answer then perform 'else'
else:
    #Prints a message and tells the user which is the right answer
    print('It is the wrong answer...The right answer is actually : ', answers[q_one_index], "\n")


#Print statement tells the user how many questions he got right out of the possible 5
print("You got ", right_q_count , "answers right out of 5!" )

您应该
zip
将这三个列表放在一起,以便同时遍历它们。此外,您还应该对压缩列表进行
random.sample
,以避免编制太多索引。通常,对
.index
的多次调用都是一种代码味道

triplets = list(zip(questions, possible_answers, answers)

num_of_rand_q = 5
right_q_count = 0
list_of_rand_q = random.sample(triplets, num_of_rand_q)

for question, choices, answer in list_of_rand_q:
    print(question)
    print(choices, "\n")

    guess = input("Type the city name of which you think it's the capital! \n")

    if guess == answer:
        print('Well done! This is the right answer!\n')
        right_q_count += 1
    else:
        print('It is the wrong answer...The right answer is actually : ', answer, "\n")

print("You got ", right_q_count , "answers right out of 5!" )

正如您所见,它也更直观,因为您不需要进行所有订阅。

您的for循环应该如下所示:

for question in list_of_rand_q:
    print(question)
或者在另一种情况下:

sum = 0
for number in range(0,n):
    sum = sum + number
在python中,第一个变量(上述情况中的问题或数字)是for循环在第二个结构(list_of_rand_q或range)中迭代时找到的对象的临时名称


换句话说,当打印问题时,不需要给出列表中问题的索引,因为for循环已经找到并创建了问题的临时引用。

无论最终得到什么解决方案,这都是令人讨厌的:
for i In list_of_rand_q[i]
-您告诉Python应该为
i
分配
list\u of\u rand\u q[i]
的每个值。因此,当循环更改时,您使用它将列表与以前的值进行索引。谢谢!我不知道我能把这三张单子拉出来。这看起来确实是一个更好的方法。