我有一些代码要求用户猜测计算的答案,然后要么告诉他们他们是正确的,要么试图找出他们出错的地方。我在此使用了一个while循环,但有时它会卡住,是否有一种方法可以对所采用的猜测添加计数器,并在5次错误的猜测后打破while循环?添加计数器while循环python
0
A
回答
1
一般来说,应该是这样的:
i = 0
while i < max_guesses:
i+=1
# here is your code
0
你只需要创建一个wrong_guess
计数器,并停止while循环,如果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
1
的Pythonic方式是
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
这样循环最多执行max_guesses
次。 else
块仅在循环未因语句break
而结束时执行,即当没有正确的猜测时。
注意if not guessed
最后是为了迎合上次错误的猜测,因为循环以wrong_guesses ==(max_guesses - 1)结束。这是因为range
是区间[0,max_guesses)(不包括上限)的迭代器。
1
只需创建一个变量来存储不正确的猜测和使用如果在5个incorrects发生,以决定条件,停止如下所示的loop.As:
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
相关问题
- 1. 添加while和for循环
- 2. 添加分钟while循环
- 3. Python - While循环
- 4. Python的while循环while循环
- 5. 添加div arround while while循环
- 6. 在while循环中添加数字
- 7. SQL服务器添加计算列与WHILE循环
- 8. 使用While循环在Python中添加/添加文件
- 9. python basic while循环
- 10. SQLite/python - 在while循环内添加数据列
- 11. 使用While循环(Python)计算GPA
- 12. 计数器内部while循环每X次增加
- 13. 简单的计算器和while循环
- 14. SQL while循环与日期计算器
- 15. Python 3.4:while循环不循环
- 16. while循环python导致无限循环
- 17. 在while循环结尾添加`;`
- 18. PHP在while循环中添加+1
- 19. 添加组件for或while循环CodenameOne
- 20. 将if语句添加到while循环
- 21. 在while循环添加到NSDictionary中
- 22. 添加到日期while循环
- 23. GPA计算器/在循环外添加
- 24. 夸克计算while循环
- 25. 的Javascript while循环计算
- 26. PHP在while循环添加计数器,并返回到一个新行,当我到达这个计数器
- 27. while while循环
- 28. while while循环
- 29. 如果在while循环多个条件和计时器python
- 30. Python列表在While循环
有没有必要写'wrong_guess + = 1'这是因为当答案是正确的时OP被打破。 – MaLiN2223