-3

I want the code I currently have to go through a list of questions infinitely or until someone gets an answer wrong. I am currently using

random.shuffle(questions)

for question in questions:
    question.ask()

to ask every question in a list once.

How do I make it continuously ask until the user inputs a wrong answer? Here is my current code:

class Question(object):
    def __init__(self, question, answer):
        self.question = question
        self.answer = answer

    def ask(self):

        response = input(self.question)
        if response == self.answer:
            print "CORRECT"
        else:
            print "wrong"

questions = [
    Question("0", 0),
    Question("π/6", 30), 
    Question("π/3", 60),
    Question("π/4", 45),
    Question("π/2", 90),
    Question("2π/3", 120),
    Question("3π/4", 135),
    Question("5π/6", 150),
    Question("π", 180),
    Question("7π/6", 210),
    Question("4π/3", 240),
    Question("5π/4", 225),
    Question("3π/2", 270),
    Question("5π/3", 300),
    Question("7π/4", 315),
    Question("11π/6", 330),
    Question("2π",360),
]

Also, if you could tell me how to add one score for every question correct that would be much appreciated. I tried to do this but I already have a piece of the program that deducts 1 from a global score variable every 5 seconds. I would like to continue editing that same variable but it gives errors.

Warstolrem
  • 131
  • 1
  • 12
  • Is that really your indentation? – Andrew Li Jul 26 '16 at 19:58
  • 2
    Possible duplicate of [Loops in Python 3.4.3](http://stackoverflow.com/questions/31445050/loops-in-python-3-4-3) or any number of "keep asking the user until ___" questions [1](http://stackoverflow.com/questions/20337489/python-how-to-keep-repeating-a-program-until-a-specific-input-is-obtained), [2](http://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response), [3](http://stackoverflow.com/questions/8114355/loop-until-a-specific-user-input), [4](http://stackoverflow.com/questions/12556907/continually-prompting-user-for-input-in-python), etc. – TessellatingHeckler Jul 26 '16 at 20:04
  • Oh, my bad, it seems to have messed up when I copied over, I will fix – Warstolrem Jul 26 '16 at 20:04

2 Answers2

0

you could loop through the list with a while loop something like this possibly

score = 0
currIndex = 0

#ask a question to start off
q1 = questions[currIndex]
#get the answer
answer = q1.ask()
while(answer == q1.answer):
    #ask the question at this index
    score+=1 
    q1=questions[currIndex]
    answer = q1.ask()

    currIndex+=1
    #reset the loop?
    if currIndex == len(questions)-1:
        currIndex = 0

haven't tested it yet but this should work? It will go until they get the answer wrong otherwise infinitely. edit: whoops didn't read that completely, I would make ask return correct or wrong and then change the loop to

 while (answer == "CORRECT"):
ynot269
  • 305
  • 2
  • 14
0

It might be worth a try giving ask() a return value. True if the answer was correct and False if the answer was incorrect. That could look like this:

def ask(self):
    response = input(self.question)
    if response == self.answer:
        print "CORRECT"
        return True
    else:
        print "wrong"
        return False

Then you could iterate through the questions like that:
(You would firstly have to create a score variable!)

for q in questions:
    if q.ask() is True:
        score += 1
    else:
        break #Breaks out of the while loop

Eitherway, you will have to make your answers Strings too in order not to compare a String to an Integer (which will never be the same), so questions should look like this:

questions = [
    Question("0", "0"),
    Question("π/6", "30"), 
    Question("π/3", "60"),
    Question("π/4", "45"),
    ...

I hope I could help you!

Agilix
  • 314
  • 1
  • 13