在while循环python中添加一个计数器

时间:2017-02-04 13:21:05

标签: python while-loop

我有一些代码要求用户猜测计算的答案,然后告诉他们他们是正确的或者试图找出他们出错的地方。我在这里使用了一个while循环,但有时会卡住,有没有办法在猜测中添加一个计数器,并在5次错误的猜测后打破while循环?

5 个答案:

答案 0 :(得分:2)

一般来说,它应该是这样的:

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

答案 1 :(得分:2)

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]上的迭代器(不包括上限)。

答案 2 :(得分:1)

只需创建一个变量来存储不正确的猜测并使用if条件来决定何时发生5个incorrects,停止循环。如下所示:

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

答案 3 :(得分:0)

您只需创建一个wrong_guess计数器,并在wrong_guess&gt; = 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

答案 4 :(得分:0)

通常我这样做:

import itertools as it
counter = it.count()
while True:
    print(next(counter))
相关问题