#π Need help with a function im trying to create
45 messages Β· Page 1 of 1 (latest)
@maiden mist
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.
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
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.
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.
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?
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.
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
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()
I don't see why this wouldn't make sense
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
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
This is your original code
See my corrected code again: #1358215168076808475 message
oh wait
mb
i forgot to indent the while loop
it works as intended now
Thank you
You're welcome. If you need an explanation, let me know.
i would like an explanation
i still dont know why the while loop has to be inside the for loop
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")
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | Letter: a
002 | Letter: b
003 | Letter: c
004 | After loop
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
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
You're welcome.
also how do i close this help channel?
!close
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.