简体   繁体   English

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

[英]memory error or empty returned Lists in Python

The first function calculates the depth of a lake and has the volume as parameter.第一个 function 计算湖的深度并以体积为参数。 In the second one, I have to pass a volume and a time increment (that I have to increment) and it returns two lists.在第二个中,我必须传递一个音量和一个时间增量(我必须增加),它会返回两个列表。 First List: time increments and the the last time/element in this list corresponds to the volume = 0, when the lake empty is.第一个列表:时间增量,此列表中的最后一个时间/元素对应于体积 = 0,此时湖为空。 Second list: the corresponding depth to each time.第二个列表:每次对应的深度。 For example for dt = 1000s seconds, z(depth) = 10m and for dt = 150000s, dt = 0m.例如,对于 dt = 1000s 秒,z(depth) = 10m,对于 dt = 150000s,dt = 0m。 Sorry, it's too much fluid mechanics, I just want to make it as clear as possible.抱歉,流体力学太多了,我只想说清楚一点。 When I call the function abfluss, it returns empty Lists when V0 is little or and it shows a memory error when v0 is bigger.当我调用 function abfluss 时,它会在 V0 较小时返回空列表,或者在 v0 较大时显示 memory 错误。 I tried with many different vo and dt, I never had a filled list, it happens only when I try it without the while loop (then I have 1 element in each List).我尝试了许多不同的 vo 和 dt,我从来没有一个填充列表,只有当我在没有 while 循环的情况下尝试它时才会发生(然后我在每个列表中有 1 个元素)。 Any suggestions?有什么建议么?

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

recalculate V_geblieben inside loop and it works在循环内重新计算 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))

this returns two lists from the function.这会从 function 中返回两个列表。 I made numerous changes to the code, please let me know if you have any questions about the code.我对代码进行了许多更改,如果您对代码有任何疑问,请告诉我。

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