Python之数学题目练习

首先,下面的题目来自我的大学同学的分享,他用数学证明,我用编程计算机发现了答案。

他的数学推理:

然后下面是我的Python代码:

#coding=utf-8

# 井的高度
well_hegith = 12

print ('井的高度是%ld'%(well_hegith))

# 白天
def daytime(allHeight):
    allHeight = allHeight + 5
    if allHeight>well_hegith:
        allHeight = -1
    else:
        allHeight = allHeight * 0.8
    return allHeight


# 黑夜
def night(allHeight):
    allHeight = allHeight + 3
    if allHeight>well_hegith:
        allHeight = -1
    else:
        allHeight = allHeight*2.0/3.0
    return allHeight


# 白天开始算,是不是白天,默认开始是白天
isDayTime = True
# 总的高度,默认开始是0
allHeight = 0
# 天数
days = 0.0

while allHeight >= 0:
    print days
    days = days + 0.5
    if isDayTime:
        allHeight = daytime(allHeight)
        isDayTime = False
    else:
        allHeight = night(allHeight)
        isDayTime = True
    print ('高度:=======%lf'%(allHeight))

 我的程序最后跑出来进入了无限循环中。。。

原文地址:https://www.cnblogs.com/goodboy-heyang/p/7040010.html