只需创建一个变量来存储不正确的猜测,并使用if条件来决定何时发生5个不正确,就可以停止循环。如下所示: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