我有一些代碼要求用戶猜測計算的答案,然後要麼告訴他們他們是正確的,要麼試圖找出他們出錯的地方。我在此使用了一個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