Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
While Loop
#1
Hello,
Kindly help me with the following. I am still trying to figure out loops and the While statement. Please tell me how to go about code for the following question and why my code is wrong. It is from the edx (Microsoft) online course.
Thanks.

Question
create a function: quiz_item() that asks a question and tests if input is correct

quiz_item()has 2 parameter strings: question and solution
shows question, gets answer input
returns True if answer == solution or continues to ask question until correct answer is provided
use a while loop

create 2 or more quiz questions that call quiz_item()
Hint: provide multiple choice or T/F answers

My code
def quiz(question, solution):
    answer = input("State the solution to the quiz: ")
    if answer == solution:
        return True
    else:
        print("Wrong Answer!\nTry again...\n")
        answer = input("State the solution to the quiz: ")
        while quiz(question, solution) == False:
            print("Wrong Answer!\nTry again...\n")
Reply
#2
are you calling the function?
like:
quiz(myquestion, proper answer)
in addition, the text of:
answer = input("State the solution to the quiz: ")
should be:
answer = input(question)
Reply
#3
(Apr-30-2018, 10:20 PM)Larz60+ Wrote: are you calling the function?
like:
quiz(myquestion, proper answer)
in addition, the text of:
answer = input("State the solution to the quiz: ")
should be:
answer = input(question)

Thank you. But if my understanding holds, non of these corrections affects the code. It still would not achieve the result the question demands. I called the function as shown below.

quiz("What is the square root of 25", "5")
Reply
#4
try:
if answer.strip() == solution.strip():
Reply
#5
the quiz function is supposed to call itself?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
(May-01-2018, 02:07 AM)Larz60+ Wrote: try:
if answer.strip() == solution.strip():

Thanks.

(May-01-2018, 02:26 AM)Skaperen Wrote: the quiz function is supposed to call itself?

No.
The following is supposed to call it. I forgot to include it in the original post.
quiz("What is the square root of 25", "5")
Reply
#7
what skaperen was pointing out is that you are recursively calling quiz from within quiz!
Reply
#8
(May-01-2018, 10:44 AM)Larz60+ Wrote: what skaperen was pointing out is that you are recursively calling quiz from within quiz!

Can you please explain this?
Reply
#9
def quiz(question, solution):
    answer = input("State the solution to the quiz: ")
    if answer == solution:
        return True
    else:
        print("Wrong Answer!\nTry again...\n")
        answer = input("State the solution to the quiz: ")
        while quiz(question, solution) == False:
            print("Wrong Answer!\nTry again...\n")
Line 8 is calling quiz, the function that it's part of, (this is called recursion),
to avoid this, your while statement needs to encompass everything from answer to the end like:
def quiz(question, solution):
    answer = None
    while answer is None:
        answer = input("State the solution to the quiz: ")
        if answer != solution:
            print("Wrong Answer!\nTry again...\n")
            answer = None
    return True
Reply
#10
Line 8 is calling quiz, the function that it's part of, (this is called recursion),
to avoid this, your while statement needs to encompass everything from answer to the end like:
def quiz(question, solution):
    answer = None
    while answer is None:
        answer = input("State the solution to the quiz: ")
        if answer != solution:
            print("Wrong Answer!\nTry again...\n")
            answer = None
    return True
[/quote]

Thank you!
It worked. Now I have to get familiar with the None keyword.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020