2017-02-04 3 views
0

Ich habe einen Code, der den Benutzer auf die Antwort auf eine Berechnung raten lässt, und dann entweder sagt ihnen, dass sie richtig sind oder versucht zu identifizieren, wo sie falsch gelaufen sind. Ich habe eine while-Schleife in diesem verwendet, aber manchmal bleibt es stecken, gibt es eine Möglichkeit, einen Zähler zu den Annahmen hinzuzufügen, und die while-Schleife nach 5 falschen Schätzungen zu brechen?Hinzufügen eines Leistungsindikators zur While-Schleife python

Antwort

1

Generell sollte es so aussehen:

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

Sie brauchen nur einen wrong_guess Zähler zu erstellen, und die while-Schleife zu stoppen, wenn 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 
+2

Es besteht keine Notwendigkeit für das Schreiben von 'ist wrong_guess + = 1 ' so oft, seit OP bricht, wenn die Antwort richtig ist. – MaLiN2223

1

Der Pythonic Weg ist

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 

Auf diese Weise wird die Schleife höchstens 0 ausgeführtmal. Der Block else wird nur ausgeführt, wenn die Schleife wegen einer Anweisung break nicht beendet wurde, d. H. Wenn keine korrekte Schätzung vorhanden war.

Beachten Sie die if not guessed am Ende ist für das Zählen der letzten falschen Schätzung gerecht zu werden, weil die Schleife mit false_guesses == (max_guesses - 1 in diesem Fall) endet. Dies liegt daran, dass ein Iterator über das Intervall [0, max_guesses] ist (mit Ausnahme der oberen Grenze).

1

Erstellen Sie einfach eine Variable falsche Vermutungen zu speichern und zu verwenden ein, wenn die Bedingung zu entscheiden, wann 5 incorrects auftreten, die loop.As unten stoppen:

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 
Verwandte Themen