while循环比较两个变量

时间:2017-09-20 05:23:45

标签: python-3.x while-loop

我试图找到并打印满足ca> = cb的n的最小值:

ap = 80000
bp = 200000
at = 1.03
bt = 1.015
n = 1
ca = ap*at*n
cb = bp*bt*n

while cb > ca:
    n = n + 1
    print(n)

代码只是运行并无限期地打印n + 1。解决这个问题的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

ap = 80000
bp = 200000
at = 1.03
bt = 1.015
n = 1
ca = ap*at*n
cb = bp*bt*n

while cb > ca:
  n += 1
  ca = ap*at*n
  cb = bp*bt*n

虽然这不会收敛。