简体   繁体   English

如何进行循环以从列表中获取特定项目? - python

[英]how to make a loop to get specific items from a list? - python

about the program:关于程序:

in this program I need to get user's input for making a 'quiz' first asks for the no.在这个程序中,我需要获得用户的输入以进行“测验”,首先要求否。 of questions and then asks the question text and 4 'answers', and ask for the correct answer from those 4 answers.问题,然后询问问题文本和 4 个“答案”,并从这 4 个答案中要求正确答案。 repeats for the no.重复没有。 of questions.的问题。

like a 'quiz maker'像一个“测验制作者”

list_of_questions stores the text for questions list_of_questions 存储问题的文本

list_of_keys stores the inputed 'answers' (in order) for all questions Example: list_of_keys 存储所有问题的输入“答案”(按顺序)示例:

list_of_keys = [Q1_Answer1, Q1_Answer2, Q1_Answer3, Q1_Answer4, Q2_Answer1, Q2_Answer2, Q2_Answer3, Q2_Answer4, Q3_Answer1 etc...]

In start_quiz() I need to first print the 1st question text and the 4 answers and then 2nd question text and the 4 answers etc...在 start_quiz() 我需要先打印第一个问题文本和 4 个答案,然后打印第二个问题文本和 4 个答案等...

using a loop to get the specific items from the list使用循环从列表中获取特定项目

BUT the loop I have made below: just prints the same first 4 answers over again.但是我在下面制作的循环:只需再次打印相同的前 4 个答案。

I can't find a solution to get the items correctly from the list.我找不到从列表中正确获取项目的解决方案。

I tried this but it didn't work: (out of range error)我试过了,但没有用:(超出范围错误)

var1 = 0
for I in main_list:
    answer_amount = 4
    if answer_amount > 0:
        var1 += 1
        print(list_of_keys[var1])

the entire code:整个代码:

from questionclass import Question_template

main_list = []

list_of_questions = []
list_of_answers = []
list_of_keys = []


print("\n Quiz maker \n")


def make_quiz():
    X = 0
    Z = 0
    Y = 0
    amount_of_answers = 4
    question_amount = int(input(" Enter number of questions for     your quiz: "))
    while question_amount > 0:
        question_amount -= 1
        X += 1
        list_of_questions.append(str(input(" Text for question no." + str(X) + ": ")))
        while amount_of_answers > 0:
            amount_of_answers -= 1
            Y += 1
            list_of_keys.append((str(Y) + ".") + str(input("  Answer no." + str(Y) + ": ")))
        amount_of_answers = 4
        list_of_answers.append(int(input("\nwhich answer is the correct answer?\n")))
        Y = 0
    for question in list_of_questions:
        main_list.append(Question_template(question, list_of_keys[Z], list_of_answers[Z]))
        Z += 1
    start_quiz()

def start_quiz():
    key = int(input("\n enter 0 to start quiz: "))
    score = 0
    If key == 0:
        for question in main_list: 
            print(question.promt) # printing the question text
            amount_of_answers = 4


            for i in list_of_keys:     ####THIS LOOP HERE#### 
                if amount_of_answers > 0:
                    print(i)
                    amount_of_answers -= 1


            answer = int(input(""))
            if answer == list_of_answers[0]:
                score += 1

makequiz()

question class:问题 class:

class Question_template:
    def __init__(self, promt, answers, correct):
        self.promt = promt
        self.answers = answers
        self.correct = correct

You get the every time same answers due to fact that you iterate every time from starting point of the list_of_keys.由于您每次都从 list_of_keys 的起点进行迭代,因此您每次都会得到相同的答案。 For question 1 you iterate between 0 to 4 and for question 2 you iterate between 0 4 again.对于问题 1,您在 0 到 4 之间迭代,对于问题 2,您再次在 0 4 之间迭代。 However, your information is between 4 and 8 for question 2. Therefore, you should add a key counter that stores which question you are looking for.但是,对于问题 2,您的信息介于 4 到 8 之间。因此,您应该添加一个密钥计数器来存储您要查找的问题。 Therefore, I have added keyCounter, and increased it to 4 for every question.因此,我添加了 keyCounter,并为每个问题将其增加到 4。 In addition to this you should also start from that point in the list_of_keys like list_of_keys[keyCounter:].除此之外,您还应该从 list_of_keys 中的那个点开始,例如 list_of_keys[keyCounter:]。

def start_quiz():
    key = int(input("\n enter 0 to start quiz: "))
    score = 0
    keyCounter = 0
    if key == 0:
        for question in main_list:
            print(question.promt) # printing the question text
            amount_of_answers = 4

            for i in list_of_keys[keyCounter:]:     ####THIS LOOP HERE####
                if amount_of_answers > 0:
                    print(i)
                    amount_of_answers -= 1
            keyCounter = keyCounter + 4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM