循环输出到一个列表或numpy数组,也是一个nonetype

2024-07-07 01:00:31 发布

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

这是我的密码:

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

我花了很长时间才赶上这个阶段,但它仍然不是我想要的

我想为变量rt和ht的循环每次迭代得到一个值列表或数组。目前,这可以工作并为循环打印正确的值,但也会导致非类型错误。如果我添加一个return rt, ht, counter,那么结果是一个值,而不是一个列表/数组

所以我看到了this的答案,但理想情况下,我希望这个函数包含在一个函数中,而我看不到应该在循环中的什么地方使用列表

任何帮助都将不胜感激!谢谢


Tags: nonemodenpcounterithihtprint
2条回答

您忘记返回三值元组:

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

否则将不返回任何内容,并且无法打开包装

    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?

相关问题 更多 >