小编Jul*_*lie的帖子

如何在 while 循环中添加计数器?

我有一些代码要求用户猜测计算的答案,然后告诉他们他们是正确的或尝试找出他们出错的地方。我在其中使用了 while 循环,但有时它会卡住,有没有办法为所进行的猜测添加计数器,并在 5 次错误猜测后中断 while 循环?这是到目前为止我的代码的一部分:

\n
#define correct answer for A\nAc=L*xm\n#ask user to work out A (monthly interest * capital)\nwhile True:\n    A= raw_input("What do you think the monthly interest x the amount you are borrowing is? (please use 2 d.p.) \xc2\xa3")\n    A=float(A)\n    #tell user if they are correct or not\n    if A==round(Ac,2):\n        print("correct")\n        break\n    elif A==round(L*x,2):\n        print("incorrect. You have used the APR rate, whic is an annual rate, you should have used this rate divided by 12 to …
Run Code Online (Sandbox Code Playgroud)

python while-loop

6
推荐指数
3
解决办法
6万
查看次数

标签 统计

python ×1

while-loop ×1