Python 如何向while循环添加计数器?

Python 如何向while循环添加计数器?,python,while-loop,Python,While Loop,我有一些代码,要求用户猜测计算的答案,然后告诉他们答案是正确的,或者试图找出错误的地方。我在这篇文章中使用了while循环,但有时它会卡住,有没有办法为猜测添加一个计数器,并在5次错误猜测后打破while循环 一般来说,它应该是这样的: i = 0 while i < max_guesses: i+=1 # here is your code i=0 而我猜: i+=1 #这是你的密码 一般来说,它应该是这样的: i = 0 while i < max_guesse

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

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

i = 0
while i < max_guesses:
    i+=1
    # here is your code
i=0
而我猜:
i+=1
#这是你的密码

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

i = 0
while i < max_guesses:
    i+=1
    # here is your code
i=0
而我猜:
i+=1
#这是你的密码

您只需创建一个
错误猜测
计数器,并在
错误猜测
>=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
错误\u猜测=0
Ac=L*xm
#请用户算出一个(月利息*资本)
如果猜错了_<5:
A=原始输入(“您认为每月利息x您借款的金额是多少?(请使用2 d.p.))
A=浮动(A)
#告诉用户它们是否正确
如果A=四舍五入(Ac,2):
打印(“正确”)
打破
elif A==四舍五入(L*x,2):
打印(“不正确。您使用的是APR费率,它是一种年费率,您应该使用此费率除以12,使其成为每月费率”)
elif A==四舍五入(L/(x*100),2):
打印(“不正确。您本应将利率用作小数点时,却将其用作整数,并将其除以12作为月利率”)
其他:
打印(“错,好像你在什么地方出错了,你应该把贷款金额乘以月利率”)
错误的猜测+=1

您只需创建一个
错误猜测
计数器,并在
错误猜测
>=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
错误\u猜测=0
Ac=L*xm
#请用户算出一个(月利息*资本)
如果猜错了_<5:
A=原始输入(“您认为每月利息x您借款的金额是多少?(请使用2 d.p.))
A=浮动(A)
#告诉用户它们是否正确
如果A=四舍五入(Ac,2):
打印(“正确”)
打破
elif A==四舍五入(L*x,2):
打印(“不正确。您使用的是APR费率,它是一种年费率,您应该使用此费率除以12,使其成为每月费率”)
elif A==四舍五入(L/(x*100),2):
打印(“不正确。您本应将利率用作小数点时,却将其用作整数,并将其除以12作为月利率”)
其他:
打印(“错,好像你在什么地方出错了,你应该把贷款金额乘以月利率”)
错误的猜测+=1
方法是

这样,循环最多执行
max_猜测次数。只有当循环由于
break
语句(即没有正确的猜测)而没有结束时,才会执行
else

请注意,结尾处的
if not guessed
用于计算最后一次错误猜测,因为循环以错误的猜数==(在这种情况下,最大猜数为-1)结束。这是因为
range
是区间[0,最大猜测数(不包括上限)上的迭代器

这样循环最多执行
max_guess
次。只有当循环由于
break
语句(即没有正确的猜测)而没有结束时,才会执行
else


请注意,结尾处的
if not gusted
用于计算最后一次错误猜测,因为循环以错误的猜测==(在这种情况下,最大猜测数为-1)结束。这是因为
range
是区间[0,最大猜测数]的迭代器(不包括上限).

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

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

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

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
通常我会这样做:

按需要导入itertools
counter=it.count()
尽管如此:
打印(下一个(计数器))
通常我会这样做:

按需要导入itertools
counter=it.count()
尽管如此:
打印(下一个(计数器))

没有必要写
错误的猜测+=1
这么多次,因为OP在答案正确时会中断。没有必要写
错误的猜测+=1
这么多次,因为OP在答案正确时会中断。