#πŸ”’ Need help with a function im trying to create

45 messages Β· Page 1 of 1 (latest)

maiden mist
#

Im trying to make a quiz, but whenever i run it I get all the questions at once when I want it to come one by one.

naive houndBOT
#

@maiden mist

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

maiden mist
#
def do_quiz():
    with open("questions.txt") as text:
        lines = text.readlines()
    
    if len(lines) < 60:
        print("Not enough questions go back and add more.")
        main_menu()
        return
    
    questions = []
    count = 0
    for i in range(0, len(lines), 6):
        question = lines[i]
        answers = lines[i + 1 : i + 5]
        correct = lines[i + 5].strip()
        questions.append([question, answers, correct])
        
    random.shuffle(questions)
    
    print("Please wait while I make your quiz...")
    time.sleep(1)
    
    question_num = 1
    for q in questions[:10]:
        print("Question ", question_num)
        print(q[0])
        
        for answer in q[1]:
            print(answer)
        
    active = True
    while active:
        answer = input("Please enter your answer (A, B, C, D): ").upper()
        if answer in {"A", "B", "C", "D"}:
            if answer == q[2]:
                print("Correct answer")
                count += 1
                active = False
            else:
                print("That was the wrong answer")
                print("The correct answer was", q[2])
                active = False
        else:
            print("not in range")
    
    question_num += 1
#

this is kinda the main function in my quiz

quick hound
#

Note the indentation you have:

    question_num = 1
    for q in questions[:10]:
        print("Question ", question_num)
        print(q[0])
        
        for answer in q[1]:
            print(answer)
        
    active = True
    while active:
        answer = input("Please enter your answer (A, B, C, D): ").upper()

You aren't asking for an answer until the entire question printing loop has finished.

maiden mist
#

ohh

#

how do i fix tht?

quick hound
#

If you want to ask the user for an answer after each question, you need to put the answering loop in the question printing loop, after when the question is printed.

maiden mist
#
    
    question_num = 1
    for q in questions[:10]:
        print("Question ", question_num)
        print(q[0])
        
        for answer in q[1]:
            print(answer)
            active = True
            while active:
                answer = input("Please enter your answer (A, B, C, D): ").upper()
                if answer in {"A", "B", "C", "D"}:
                    if answer == q[2]:
                        print("Correct answer")
                        count += 1
                        active = False
                    else:
                        print("That was the wrong answer")
                        print("The correct answer was", q[2])
                        active = False
              else:
                 print("not in range")
                 question_num += 1

#

so something like this?

quick hound
#

You want it to print out all the answers, then ask for an answer. You moved the asking loop into the for answer in q1[1]` loop, so it will ask for an answer after displaying each answer.

maiden mist
#

right I don't want tht, so should i put the whole while loop inside the first for loop? but it won't make sense if i do tht because i still need to see the answers

quick hound
quick hound
maiden mist
#

let me try running it

#

if run tht, this is my output

#

but i want the questions printed one by one so i can answer them one at a time

quick hound
#

Show the new code

#

And make sure you saved the file

maiden mist
#

yup

#
question_num = 1
    for q in questions[:10]:
        print("Question ", question_num)
        print(q[0])
        
        for answer in q[1]:
            print(answer)       
            active = True
            
    while active:
        answer = input("Please enter your answer (A, B, C, D): ").upper()
        if answer in {"A", "B", "C", "D"}:
            if answer == q[2]:
                print("Correct answer")
                count += 1
                active = False
            else:
                print("That was the wrong answer")
                print("The correct answer was", q[2])
                active = False
        else:
            print("not in range")
    
    question_num += 1
quick hound
#

This is your original code

maiden mist
#

oh wait

#

mb

#

i forgot to indent the while loop

#

it works as intended now

#

Thank you

quick hound
#

You're welcome. If you need an explanation, let me know.

maiden mist
#

i would like an explanation

#

i still dont know why the while loop has to be inside the for loop

quick hound
#

A loop will run entirely before the code after the loop runs:

#

!e

for letter in ['a', 'b', 'c']:
    print('Letter:', letter)

print("After loop")
naive houndBOT
quick hound
#

Note how "After loop" doesn't print until the entire loop before it has finished (which you can see executing using the "Letter" printouts).

#

Now look at your original code:

for q in questions[:10]:
    print("Question ", question_num)
    print(q[0])
    
    for answer in q[1]:
        print(answer)
    
active = True
while active:

The while active loop, which asks the user for their answer, is after the for q in questions[:10]: loop. That means it won't run and ask the user for their answer until the entire for q in questions[:10]: loop has finished printing every question

maiden mist
#

ohh i see how it works now

#

i was wondering if my entire code was wrong for a second

#

thanks for explaining it to me

quick hound
#

You're welcome.

maiden mist
#

also how do i close this help channel?

quick hound
#

!close

naive houndBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.