mercredi 23 mars 2016

Three If statements in while loops

I have three if statements in my while loop that is supposed to ask questions until the question variable is equal to 10.

However I think because I have three if statements is the reason why it is only asking three questions instead. I have tried to use break - which ends my while loop

This is my code:

while question<10: 
user_answer=input(str(random.choice(numbers)) + random.choice(operators) + str(random.choice(numbers1))) 

if operators == '+': 
    expected_answer = numbers + numbers1 
    if user_answer==expected_answer:
        print ('This is correct!') 
        print ("Your score so far is",score,"out of 10") 
        question=question+1 
        time.sleep(2) 
        print ('This is incorrect!') 
        question=question+1 
        time.sleep(2) 

if operators == '-': 
    expected_answer = numbers - numbers1 
    if user_answer==expected_answer: 
        print ('This is correct!') 
        print ("Your score so far is",score,"out of 10") 
        question=question+1 
        time.sleep(2) 
        print ('This is incorrect!') 
        question=question+1 
        time.sleep(2) 

if operators == '*': 
    expected_answer = numbers * numbers1
    if user_answer==expected_answer: 
        print ('This is correct!') 
        score=score+1 
        print ("Your score so far is",score,"out of 10") 
        question=question+1 
        time.sleep(2) 
        print ('This is incorrect!')  
        question=question+1 
        time.sleep(2) 

Aucun commentaire:

Enregistrer un commentaire