简体   繁体   English

将输出循环到列表或numpy数组,以及一个NoneTypeError

[英]Loop outputs to a list or numpy array, also a NoneTypeError

Here's my code: 这是我的代码:

# Libs
import numpy as np

# Isostatic model:

def airy(t,tref=35.,rhoM=3.2,rhoC=2.7,mode=None,rate=None,hlimit=None):
  # Equilibrium ratios:
  er = rhoC / (rhoM-rhoC)
  di = rhoC/rhoM
  # Static buoyancy equation:
  excess = t-tref
  hi = excess * (1-di)
  ri = excess - hi  
  # Mode results:
  if mode == 'Initial':
    print("Model parameters: Crustal density =",rhoC,"Mantle density =",rhoM)
    print("Mountain height (Km) =",np.round(hi, 3))
    print("Root thickness (Km) =",np.round(ri, 3))
    print("Ratio of height to root =",er)
    return ri, hi
  elif mode=='Erosive':
    # Initialise loop
    counter = 0
    ht = hi
    while ht >= hlimit:
      counter = counter+1
      excess = t-tref
      ht = excess * (1-di)
      rt = excess - ht
      ht = ht*np.exp(rate*counter)
      t = ht+rt+tref
      print(rt, ht, counter)
  elif mode==None:
    return ri, hi

tref = 35.  
it = tref*1.5
print("Initial thickness =",it)
ir, ih = airy(it, mode='Initial')
rt, ht, tstep = airy(it, mode='Erosive', rate=-0.025,hlimit=0.5)

It's taken me bl*ody ages to get even to this stage with the loop and its still not what I'm after! 我花了很长时间才达到循环的这个阶段,而这仍然不是我追求的目标!

I would like to get a list or array of values for each iteration of the loop for the variables rt and ht. 我想为变量rt和ht的循环的每次迭代获取一个列表或值数组。 At the moment this works and prints the correct values for the loop, but also results in a NoneTypeError. 目前,这可以正常工作并为循环打印正确的值,但也会导致NoneTypeError。 If I add a return rt, ht, counter then the result is a single value, not a list/array. 如果添加return rt, ht, counter则结果是单个值,而不是列表/数组。

So I've seen this answer, but ideally want this inside one function and I can't see where I'm supposed to use a list in the loop. 因此,我已经看到了这个答案,但理想情况下是希望在一个函数中使用它,而我看不到在循环中应该在何处使用列表。

Any help would be appreciated! 任何帮助,将不胜感激! Thanks 谢谢

You forgot to return the three-valued tuple: 您忘了返回三值元组:

def airy(t,tref=35.,rhoM=3.2,rhoC=2.7,mode=None,rate=None,hlimit=None):
  # Equilibrium ratios:
  er = rhoC / (rhoM-rhoC)
  di = rhoC/rhoM
  # Static buoyancy equation:
  excess = t-tref
  hi = excess * (1-di)
  ri = excess - hi  
  # Mode results:
  if mode == 'Initial':
    print("Model parameters: Crustal density =",rhoC,"Mantle density =",rhoM)
    print("Mountain height (Km) =",np.round(hi, 3))
    print("Root thickness (Km) =",np.round(ri, 3))
    print("Ratio of height to root =",er)
    return ri, hi
  elif mode=='Erosive':
    # Initialise loop
    counter = 0
    ht = hi
    l_ht = []
    l_rt_ = []
    l_counter = []
    while ht >= hlimit:
      counter = counter+1
      excess = t-tref
      ht = excess * (1-di)
      rt = excess - ht
      ht = ht*np.exp(rate*counter)
      t = ht+rt+tref
      print(rt, ht, counter)
      l_rt.append(rt)
      l_ht.append(ht)
      l_counter .append(counter)
    return l_rt, l_ht, l_counter # HERE
  elif mode==None:
    return ri, hi

Would return None otherwise, and fail with the unpacking. 否则将返回None,并因解包失败。

    elif mode=='Erosive':
        # Initialise loop
        counter = 0
        ht = hi
        while ht >= hlimit:
          counter = counter+1
          excess = t-tref
          ht = excess * (1-di)
          rt = excess - ht
          ht = ht*np.exp(rate*counter)
          t = ht+rt+tref
          print(rt, ht, counter)
#-------> where is a return statement?

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

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