2016-12-03 65 views
1
def solution(number): 
    x = 0 
    total = 0 
    while x < number: 
    if x % 3 == 0 or x % 5 == 0: 
     total = total + x 
     x = x + 1 
    print total 
    return total 

solution(10) 

你好,当我通过IDE运行此代码时,什么也没有发生。 它有什么问题?没有任何错误或任何事情。没有功能输出

+3

'x = x + 1'位于if区块内部,所以如果条件为false,'x'永远不会增加,并且会得到一个无限循环。 – JJJ

回答

3

由于x增量问题,我认为你输入了一个无限循环。

def solution(number): 
    x = 0 
    total = 0 
    while x < number: 
    if x % 3 == 0 or x % 5 == 0: 
     total = total + x 
    x = x + 1 
    print total 
    return total 

只是增加x独立于if条件,可能会阻止其增量。

+0

这不提供问题的答案。要批评或要求作者澄清,请在其帖子下方留言。 - [来自评论](/ review/low-quality-posts/14485416) – tmthydvnprt

+0

这是问题,解决方法是他必须确保x增加if条件。 –

+0

您应该为回答帖子编写实际答案。你当前的帖子是关于你应该完成的内容的评论,而不是答案。看到这[meta post](http://meta.stackexchange.com/a/163589/292533)。 – tmthydvnprt