繁体   English   中英

memory 错误或 Python 中的返回列表为空

[英]memory error or empty returned Lists in Python

第一个 function 计算湖的深度并以体积为参数。 在第二个中,我必须传递一个音量和一个时间增量(我必须增加),它会返回两个列表。 第一个列表:时间增量,此列表中的最后一个时间/元素对应于体积 = 0,此时湖为空。 第二个列表:每次对应的深度。 例如,对于 dt = 1000s 秒,z(depth) = 10m,对于 dt = 150000s,dt = 0m。 抱歉,流体力学太多了,我只想说清楚一点。 当我调用 function abfluss 时,它会在 V0 较小时返回空列表,或者在 v0 较大时显示 memory 错误。 我尝试了许多不同的 vo 和 dt,我从来没有一个填充列表,只有当我在没有 while 循环的情况下尝试它时才会发生(然后我在每个列表中有 1 个元素)。 有什么建议么?

from numpy import pi
p = 0.036
def tiefe(V):
    a = (3*V*p**2)/pi
    z = a**(1/3)
    return z

U_unten = 35.56
A = 2                                                                       
def abfluss(V0, dt):
    
    V_geblieben = V0 - (U_unten*A*dt)/V0
    neu_tiefe = tiefe(V_geblieben)
    t_list = []
    Z_list = []
    while V_geblieben >=0:
        dt = dt+100
        t_list.append(dt)
        Z_list.append(neu_tiefe)
     
    return  t_list, Z_list
print(abfluss(50000,3600))

在循环内重新计算 V_geblieben 并且它可以工作

from math import pi

p = 0.036


def tiefe(V):
    a = (3 * V * p ** 2) / pi
    z = a ** (1 / 3)
    return z


U_unten = 35.56
A = 2


def abfluss(V0, dt):
    V_geblieben = V0 - (U_unten * A * dt) / V0
    neu_tiefe = tiefe(V_geblieben)
    t_list = []
    Z_list = []
    while V_geblieben >= 0:
        dt = dt + 100
        t_list.append(dt)
        Z_list.append(neu_tiefe)
        V_geblieben = V0 - (U_unten * A * dt) / V0
        print(V_geblieben)
    return t_list, Z_list


print(abfluss(50000, 3600))

这会从 function 中返回两个列表。 我对代码进行了许多更改,如果您对代码有任何疑问,请告诉我。

 ##This program outputs the depth of a draining lake at different time intervals
    ##Imports 
    from math import *
    ##Function                                                                     
    def abfluss(V0,dt):##functon that takes the intial volume of the lake and a time interval 
        t_list = []
        Z_list = []
        while V0 >=0:
            z= ((3*V0*0.036**2)/pi)**(1/3)
            t_list.append(dt)
            Z_list.append(z)
            U_unten = 35.56 
            V0 = V0 - (U_unten*2*dt)/V0
            dt = dt+0.01
        return  t_list, Z_list
    ##Main Program 
    V0 = float(input('Please enter an initail volume for the lake:')) ##Take initial lake volume from user in *define units for volume*
    dt = float(input('Please enter a time interval for dt:'))
    res_abfluss = abfluss(V0,dt) ##passes the user inputs to the finction 
    print(res_abfluss) ##prints the two resulting of the lists of the fucniton

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM