Julie
Julie

Reputation: 91

How to add a counter to a while loop?

I have some code which asks the user to guess the answer to a calculation, and then either tells them they are correct or tries to identify where they went wrong. I have used a while loop in this but sometimes it gets stuck, is there a way to add a counter to the guesses taken, and to break the while loop after 5 incorrect guesses? Here is a section of my code so far:

#define correct answer for A
Ac=L*xm
#ask user to work out A (monthly interest * capital)
while True:
    A= raw_input("What do you think the monthly interest x the amount you are borrowing is? (please use 2 d.p.) £")
    A=float(A)
    #tell user if they are correct or not
    if A==round(Ac,2):
        print("correct")
        break
    elif A==round(L*x,2):
        print("incorrect. You have used the APR rate, whic is an annual rate, you should have used this rate divided by 12 to make it monthly")
        continue
    elif A==round(L/(x*100),2):
        print("incorrect. You have used the interest rate as a whole number when you should have used it as a decimal, and divided it by 12 for the monthly rate")
        continue
    else:
        print("Wrong, it seems you have made an error somewhere, you should have done the loan amount multiplied by the monthly rate")
        continue

Upvotes: 7

Views: 64359

Answers (4)

rassi
rassi

Reputation: 385

The standard library's itertools provide an iterator counting up:

import itertools

for i in itertools.count():
    print(i)

You can set the start and step:

# count(10) --> 10 11 12 13 14 ...
# count(2.5, 0.5) --> 2.5 3.0 3.5 ...

Upvotes: 8

miraculixx
miraculixx

Reputation: 10349

The Pythonic way is

max_guesses = 5
solution = ... # Some solution
guessed = False
for wrong_guesses in range(1, max_guesses + 1):
    guess = ... # Get Guess
    if guess == round(solution, 2):
        print("correct")
        guessed = True
        break 
    ...
else:
    print(f"You have exceeded the maximum of {max_guesses} guesses") 

This way the loop is executed at most max_guesses times. The else block is only executed if the loop did not end because of a break statement i.e. when there was no correct guess.

Note the max_guesses + 1 because range is an iterator over the interval [low, high) (excluding the upper limit). So to run from 1 to max_guesses we have to +1.

Upvotes: 6

Taufiq Rahman
Taufiq Rahman

Reputation: 5704

Just create a variable to store the amount of incorrect guesses and use an if condition to detect when user guessed wrong 5 times, then stop the loop. As shown below:

Ac=L*xm
incorrect_guesses = 0 # variable to store incorrect guesses
# ask user to work out A (monthly interest * capital)
while True:
    if incorrect_guesses == 5:
        break # stop loop
    else: # if not, continue normally
        guess = raw_input("What do you think the monthly interest x the amount you are borrowing is? (please use 2 d.p.) £")
        guess = float(guess)
        # tell user if they are correct or not
        if guess == round(Ac, 2):
            print("correct")
            break
        elif guess == round(L * x, 2):
            print("incorrect. You have used the APR rate, which is an annual rate, you should have used this rate divided by 12 to make it monthly")
            incorrect_guesses += 1
        elif A == round(L / (x * 100), 2):
            print("incorrect. You have used the interest rate as a whole number when you should have used it as a decimal, and divided it by 12 for the monthly rate")
            incorrect_guesses += 1
        else:
            print("Wrong, it seems you have made an error somewhere, you should have done the loan amount multiplied by the monthly rate")
            incorrect_guesses += 1

Upvotes: 2

MaLiN2223
MaLiN2223

Reputation: 1299

In general it should look like this:

i = 0
while i < max_guesses:
    i+=1
    # here is your code

Upvotes: 4

Related Questions