3

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?

Gino Mempin
  • 19,150
  • 23
  • 79
  • 104
Julie
  • 51
  • 1
  • 1
  • 5

5 Answers5

5

In general it should look like this:

i = 0
while i < max_guesses:
    i+=1
    # here is your code
MaLiN2223
  • 1,237
  • 3
  • 21
  • 37
5

The Pythonic way is

max_guesses = 5
guessed = False
for wrong_guesses in range(max_guesses):
    if A==round(Ac,2):
      print("correct")
      guessed = True
      break 
    ...
else:
  print("You have exceeded the maximum of {} guesses".format(max_guesses)) 
if not guessed:
  wrong_guesses += 1

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 if not guessed at the end is to cater for counting the last incorrect guess because the loop ends with wrong_guesses == (max_guesses - 1 in that case). This is because range is an iterator over the interval [0, max_guesses) (excluding the upper limit).

miraculixx
  • 9,418
  • 2
  • 37
  • 57
3

Just create a variable to store incorrect guesses and use a if condition to decide when 5 incorrects occur,stop the loop.As shown below:

Ac=L*xm
count = 0 #variable to store incorrect guesses
#ask user to work out A (monthly interest * capital)
while True:
    if count == 5: #IF COUNT(incorrect) is 5 times
        break #stop loop
    else: # if not continue normally
        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")
            count += 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")
            count += 1
        else:
            print(
                "Wrong, it seems you have made an error somewhere, you should have done the loan amount multiplied by the monthly rate")
            count += 1
Taufiq Rahman
  • 5,436
  • 2
  • 35
  • 43
2

Usually I do this:

import itertools as it
counter = it.count()
while True:
    print(next(counter))
rassi
  • 195
  • 8
0

You just need to create a wrong_guess counter, and stop the while loop if wrong_guess >= 5:

wrong_guess = 0
Ac=L*xm
#ask user to work out A (monthly interest * capital)
while wrong_guess < 5:
    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")
    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")
    else:
        print("Wrong, it seems you have made an error somewhere, you should have done the loan amount multiplied by the monthly rate")
    wrong_guess += 1
Huy Vo
  • 2,318
  • 6
  • 26
  • 43
  • 2
    There is no need for writing `wrong_guess += 1` such many times since OP is breaking when answer is correct. – MaLiN2223 Feb 04 '17 at 13:33