问题
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?
回答1:
In general it should look like this:
i = 0
while i < max_guesses:
i+=1
# here is your code
回答2:
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
回答3:
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).
回答4:
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
来源:https://stackoverflow.com/questions/42040868/add-a-counter-to-while-loop-python