Python 无法使用递归解决代码战问题

Python 无法使用递归解决代码战问题,python,Python,我试图实现一个问题的递归解决方案。我看不出我错在哪里。这个问题甚至可以用递归来解决吗 """ A child is playing with a ball on the nth floor of a tall building. The height of this floor, h, is known. He drops the ball out of the window. The ball bounces (for example), to two-thir

我试图实现一个问题的递归解决方案。我看不出我错在哪里。这个问题甚至可以用递归来解决吗

"""
A child is playing with a ball on the nth floor of a tall building. The height of this floor, h, is known.

He drops the ball out of the window. The ball bounces (for example), to two-thirds of its height (a bounce of 0.66).

His mother looks out of a window 1.5 meters from the ground.

How many times will the mother see the ball pass in front of her window (including when it's falling and bouncing?

Three conditions must be met for a valid experiment:
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.
If all three conditions above are fulfilled, return a positive integer, otherwise return -1.

Note:
The ball can only be seen if the height of the rebounding ball is strictly greater than the window parameter.

Example:
- h = 3, bounce = 0.66, window = 1.5, result is 3

- h = 3, bounce = 1, window = 1.5, result is -1

(Condition 2) not fulfilled).
"""

def bouncingBall(h, bounce, window):
    count = 1
    h = bounce * h
    if h < window:
        return count
    else:
        count += 2
        bouncingBall(h, bounce, window)
“”“
一个孩子正在一幢高楼的第n层玩球。这层楼的高度h是已知的。
他将球扔出窗外。球反弹(例如)到其高度的三分之二(反弹0.66)。
他的母亲从离地1.5米的窗户向外看。
妈妈会看到多少次球从她的窗户前经过(包括落下和反弹的时候)?
有效实验必须满足三个条件:
以米为单位的浮动参数“h”必须大于0
浮点参数“bounce”必须大于0且小于1
浮动参数“窗口”必须小于h。
如果以上三个条件都满足,则返回一个正整数,否则返回-1。
注:
只有当反弹球的高度严格大于窗口参数时,才能看到球。
例子:
-h=3,反弹=0.66,窗口=1.5,结果为3
-h=3,反弹=1,窗口=1.5,结果为-1
(条件2)未满足)。
"""
def弹跳球(h、弹跳、窗口):
计数=1
h=反弹*h
如果h<窗口:
返回计数
其他:
计数+=2
弹跳球(h,弹跳,窗口)

任何帮助都将不胜感激。谢谢大家!

您需要在else语句之后返回递归调用,并且不应该每次调用函数时都将count设置为1

def bouncingBall(h, bounce, window, count=1):
    h = bounce * h
    if h < window:
        return count
    else:
        count += 2
        return bouncingBall(h, bounce, window, count = count)
def bouncingBall(h,反弹,窗口,计数=1):
h=反弹*h
如果h<窗口:
返回计数
其他:
计数+=2
返回弹跳球(h,弹跳,窗口,计数=计数)
如果不需要递归,还可以使用简单的while循环

def bouncingBall(h, bounce, window):
    count = -1
    while h>window and 0<bounce<1 and 0<window<h:
        h = bounce * h
        count+=2
    return count
def bouncingBall(h,反弹,窗口):
计数=-1

当h>window和0时,您需要在else语句之后返回递归调用,并且不应该在每次调用函数时将count设置为1

def bouncingBall(h, bounce, window, count=1):
    h = bounce * h
    if h < window:
        return count
    else:
        count += 2
        return bouncingBall(h, bounce, window, count = count)
def bouncingBall(h,反弹,窗口,计数=1):
h=反弹*h
如果h<窗口:
返回计数
其他:
计数+=2
返回弹跳球(h,弹跳,窗口,计数=计数)
如果不需要递归,还可以使用简单的while循环

def bouncingBall(h, bounce, window):
    count = -1
    while h>window and 0<bounce<1 and 0<window<h:
        h = bounce * h
        count+=2
    return count
def bouncingBall(h,反弹,窗口):
计数=-1
而h>窗口和0
导入数学
def弹跳球(h,弹跳,窗口):
如果h>0且0
导入数学
def弹跳球(h,弹跳,窗口):

如果h>0且0Nope,则不会给出正确的解决方案。你可以在codewars上查看,你问这个问题是否可以用递归解决,我回答了。此外,我遗漏了显然需要的检查代码。您没有明确说明您所在的网站对变量有特殊要求。无论如何,我已经编辑了答案。我没有要求任何东西,伙计:p无论如何,改变我的投票!对不起,我一定是看错了顶部的问题“这个问题能用递归解决吗?”。不用担心,很高兴我能帮忙。不,没有给出正确的解决方案。你可以在codewars上查看,你问这个问题是否可以用递归解决,我回答了。此外,我遗漏了显然需要的检查代码。您没有明确说明您所在的网站对变量有特殊要求。无论如何,我已经编辑了答案。我没有要求任何东西,伙计:p无论如何,改变我的投票!对不起,我一定是看错了顶部的问题“这个问题能用递归解决吗?”。不用担心,很高兴我能帮上忙。在第一次测试(2,0.5,1,1)中,代码大战失败了。问题还在于第四个参数是给定的,问题是它是否可以用递归来解决。我添加了一个可以通过条件的替代方案。谢谢@MitchellOlislagers。我无法完全理解递归方法,但我知道我的代码哪里出错了。感谢您的投入!第一次测试的代码大战失败:测试(2,0.5,1,1)。问题还在于第四个参数是给定的,问题是它是否可以用递归来解决。我添加了一个可以通过条件的替代方案。谢谢@MitchellOlislagers。我无法完全理解递归方法,但我知道我的代码哪里出错了。感谢您的投入!