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

2024-06-26 14:44:00 发布

您现在位置:Python中文网/ 问答频道 /正文

第一个函数计算湖泊的深度,并将体积作为参数。在第二个例子中,我必须传递一个卷和一个时间增量(我必须增量),它返回两个列表。第一个列表:当湖泊为空时,时间增量和此列表中的最后一个时间/元素对应于体积=0。第二个列表:每次对应的深度。例如,对于dt=1000s秒,z(深度)=10m,对于dt=150000s,dt=0m。对不起,这是太多的流体力学,我只想让它尽可能清楚。 当我调用函数abfluss时,当V0较小或较大时,它返回空列表,当V0较大时,它显示内存错误。我尝试了许多不同的vo和dt,我从来没有一个填充列表,只有当我在没有while循环的情况下尝试它时才会发生(然后每个列表中有一个元素)。有什么建议吗

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))

Tags: 元素列表returndef时间dtpi体积
2条回答

这将从函数中返回两个列表。我对代码做了很多修改,如果您对代码有任何疑问,请告诉我

 ##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

在循环内重新计算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))

相关问题 更多 >