为什么我的代码打印输出?

时间:2016-03-17 11:39:19

标签: python math

    multiple_one = 3.0
multiple_two = 5.0
multiple_answer = 1
while ( multiple_answer < 1000):
     number_of_multiples = 0
     checking_multiple = 1000.0
     if  (checking_multiple / multiple_one).is_integer:
         number_of_multiples += 1
     elif (checking_multiple / multiple_one).is_integer:
         number_of_multiples += 1
     checking_multiple -= 1 
print number_of_multiples

我的代码假设打印出多少倍数但仍然失败。那是为什么?

2 个答案:

答案 0 :(得分:1)

你的程序陷入无限循环。你开始在这里循环:

while ( multiple_answer < 1000):

但永远不会更新multiple_answer所以你的循环永远不会完成。

答案 1 :(得分:0)

你永远不会改变multiple_answer所以它总是小于1000,所以你会被困在while语句中的无限循环中。你想计算你成倍的次数吗?如果是这样,您想要更改

multiple_one = 3.0
multiple_two = 5.0
multiple_answer = 1
while ( multiple_answer < 1000):
     number_of_multiples = 0
     checking_multiple = 1000.0
     if  (checking_multiple / multiple_one).is_integer:
         number_of_multiples += 1
     elif (checking_multiple / multiple_one).is_integer:
         number_of_multiples += 1
     checking_multiple -= 1 
print number_of_multiples

是:

multiple_one = 3.0
multiple_two = 5.0
multiple_answer = 1
while ( number_of_multiples < 1000):
     checking_multiple = 1000.0
     if  (checking_multiple / multiple_one).is_integer:
         number_of_multiples += 1
     elif SOMETHING_DIFFERENT:
         SOMETHING_ESLE_DIFFERENT 
print number_of_multiples

这样你就不会在每次迭代中重置number_of_multiples而你正在根据迭代检查while循环。