1

I'm trying to create a basic maths program which will randomly generate a numerical question and then allows 3 attempts to complete it before it moves onto the next question however cant figure out how to make it do both together. My code currently looks like this

print "What is your name?"
score = 0
Attempt = 0
loop = True
Name = raw_input()
import random
for i in range (1,6):
        question_1_part_1 = random.randint(1,30)
        question_1_part_2 = random.randint(1,30)
        print "What is", question_1_part_1, "+", question_1_part_2, "?"
        while Attempt <3: # inputing this while loop here means it will retry the same question.
            Guess = raw_input() 
            Guess = int(Guess)
            answer = question_1_part_1 + question_1_part_2
            if Guess == answer:
                print "Well done"
                score = score + 1
            else: print "try again"
            Attempt = Attempt + 1
            if Attempt == 3: print "You failed"
        print Name, "your score is", score 

2 Answers 2

1

A simple break statement will take you out of the loop when the answer is correct.

        if Guess == answer:
            print "Well done"
            score += 1
            break
        else: print "try again"

Note the change to your assignment; this is considered a cleaner increment.

To answer your comment ... you don't use this when the person gets the question wrong. Your program logic freezes out the user after three wrong guesses total, on all questions. If you want them to have up to three guesses on every problem, then you have to reset the Attempt counter for every question. Pull that line into the outer loop:

for i in range (1,6):
    Attempt = 0
    loop = True
    question_1_part_1 = random.randint(1,30)
    question_1_part_2 = random.randint(1,30)

In the future, I strongly recommend that you program incrementally: get a few lines working before you add more. Insert tracing print statements to check that the values and program flow are correct. Don't remove them until that part of the code is solid -- and even then only comment them out until the entire program is working. Some of your problems stem from trying to write more than you can comfortably hold in your mind at once -- which is common at most levels of programming. :-)

Sign up to request clarification or add additional context in comments.

2 Comments

How can I use this when the answer is incorrect however, I am trying to make the program give the user 3 attempts at the question before it moves onto the next question. I've managed to make it go back to the beginning if they get the question correct, however if they get it wrong 3 times it either wont go back to ask a new question and ends the program or it will ask the last 4 questions instantly and not allow the user to answer before saying it is incorrect
Thank you so much, ill take that on board and do that in future.
0

Add another argument to your while loop.

while Attempt <3 and Guess != ... :
    rest of loop code

Then you are exiting your loop when they get the correct answer. Or you could break from the statement when they get the answer right.

1 Comment

How can I use this when the answer is incorrect however, I am trying to make the program give the user 3 attempts at the question before it moves onto the next question. I've managed to make it go back to the beginning if they get the question correct, however if they get it wrong 3 times it either wont go back to ask a new question and ends the program or it will ask the last 4 questions instantly and not allow the user to answer before saying it is incorrect

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.