2017-02-04 9 views
0

私は、計算の答えを推測するようにユーザに指示するコードをいくつか持っていて、それらが正しいかどうか、またはどこが間違っていたのかを特定しようとします。私はこれでwhileループを使用しましたが、時にはスタックしてしまいました。推測にカウンタを追加し、5回の間違った推測の後にwhileループを壊す方法がありますか?whileループのpythonにカウンタを追加する

答えて

1

一般的に、それは次のようになります。

i = 0 
while i < max_guesses: 
    i+=1 
    # here is your code 
0

をあなただけwrong_guessカウンタを作成する必要があり、かつwrong_guess> = 5場合はwhileループを停止します。

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 
+2

'wrong_guess + = 1 'を書くための必要はありません回答が正しければOPが壊れているので何度も。 – MaLiN2223

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ステートメント、すなわち正しい推測がないときにループが終了しなかった場合にのみ実行されます。

最後に間違った推測==(その場合はmax_guesses - 1)で終了するため、if not guessedは最後の誤った推測をカウントすることに注意してください。これは、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 
関連する問題