首页 > 解决方案 > 嵌套 if 在不工作的 Python 中有一个 while 循环

问题描述

所以我刚刚开始使用 Python,我正在练习 Codewars。我正在做这个kata,我忘记了它的名字。我编写了 if、while 和另一个 if deep 的代码。我收到了这个错误:

else:
        ^
TabError: inconsistent use of tabs and spaces in indentation

我发现代码没有任何问题,并且 else 与 if 对齐。

# our parameters are h - the height from where the ball is falling,
# bounce - the percent of bounce out of 1.0, window - where teh mother is (1.5 m)
# HOW MANY TIMES DID THE BALL GO IN FRONT OF THE WINDOW
# Float parameter "h" in meters must be greater than 0
# Float parameter "bounce" must be greater than 0 and less than 1
# Float parameter "window" must be less than h.

def bouncingBall(h, bounce, window):
    current_bounce = h
    times_seen = None
    if h > 0 and bounce > 0 and bounce < 1 and window < h:
        while current_bounce_height > window:
            current_bounce_height = current_bounce_height * bounce
            if times_seen >= 1:
                times_seen = times_seen + 2
            else:
                times_seen = times_seen + 1
        print(times_seen)
        return times_seen
    else:
        print(-1)
        return -1

bouncingBall(10, 0.66, 1.5)

并且代码可能还有其他错误,正如我刚刚开始所说的那样。不要在意这些。

标签: pythonif-statementwhile-loopnested-if

解决方案


推荐阅读