Python &引用;属性错误:';非类型'';写'&引用&&引用;运行时警告:溢出双“U标量”在while循环期间接收

Python &引用;属性错误:';非类型'';写'&引用&&引用;运行时警告:溢出双“U标量”在while循环期间接收,python,python-3.x,while-loop,runtime-error,attributeerror,Python,Python 3.x,While Loop,Runtime Error,Attributeerror,我用一个while循环来获得放射性元素的向后衰变曲线 import numpy as np import csv lmbd=np.log(2)/12.43 year=list(range(-48050,2012)) f=0 decay=[] decay.append(0.45) # concentration of sample during year of sampling while f<len(year): # Creating the backwards decay cu

我用一个while循环来获得放射性元素的向后衰变曲线

import numpy as np
import csv

lmbd=np.log(2)/12.43
year=list(range(-48050,2012))

f=0
decay=[] 
decay.append(0.45)  # concentration of sample during year of sampling

while f<len(year): # Creating the backwards decay curve sample
    p=decay[f]
    x=p/np.exp(-lmbd)
    decay.append(x)
    f=f+1

print(decay)
文件“C:\Python32\lib\idlelib\PyShell.py”,第59行,在idle\u showwarning中 file.write(warnings.formatwarning(消息、类别、文件名、

AttributeError:“非类型”对象没有属性“写入”

但是,如果我将
len(year)
替换为
50
,则循环运行平稳。如果我将
len(year)
替换为
50062
(len(year)的值),我仍然会得到错误

  • 错误可能是由于迭代的长度造成的吗?(当然不是这样)
  • 为什么它只适用于50而不适用于50062
  • 当我在50000年内创造指数增长时,数字会变得太大吗?(我想保留这么多年,因为我对其他元素使用相同的脚本,这些元素的
    lmbd
    值较小,所以值不会变得太大)
  • 有修复的可能性吗
  • 感谢PaF、User和eryksun(在评论中)提供了答案,我正在使用Python 3.2:

  • 关于脚本,我得到了错误的警告(空闲)

    • 如果我在
      cmd
      中运行脚本(根据用户建议),我会得到
      运行时警告:在双标量x=p/np.exp(-lmbd)
      错误中遇到溢出

    • 另外,如果我在脚本中添加了
      import sys,idlelib.PyShell;idlelib.PyShell.warning_stream=sys.stderr
      (根据eruksun的建议),我也会在IDLE中得到正确的错误(我正在使用)

    • 脚本现在可以运行到完成,但是衰减列表的末尾有大量的
      inf
      值,这些值将在下一点中处理

  • 要处理溢出问题,请执行以下操作:


    • 我尝试将
      while
      语句替换为
      while fI刚刚复制并粘贴了您的代码,它对我来说运行良好。我得到了
      运行时警告:双标量中遇到了溢出
      。为了避免溢出,我在
      inf
      处停止,方法是更改循环条件:
      while fI get
      运行时警告:ov在double_scalars x=p/np.exp(-lmbd)
      中遇到的erflow这不是您的问题中的错误,而是希望显示错误的环境“Python Shell”中的错误。如果您在没有Python Shell的情况下执行此操作,您可能会看到原始错误。如果您使用“-n”(无子进程)空闲运行如果没有用于
      stderr
      的控制台/终端,您可以尝试以下操作来查看警告:
      import sys,idlelib.PyShell;idlelib.PyShell.warning\u stream=sys.stderr
      。在这种情况下
      sys.stderr
      是一个
      PyShell.PseudoOutputFile
      x=p/np.exp(-lmbd)  
      
      import numpy as np
      import csv
      import sys, idlelib.PyShell; idlelib.PyShell.warning_stream = sys.stderr
      
      lmbd=np.log(2)/12.43
      year=list(range(-48050,2012))
      
      f=0
      decay=[] 
      decay.append(0.45)# concentration of sample during year of sampling
      
      while f<len(year):  # Creating the backwards decay equation for each sample
          p=decay[f]
          x=p/np.exp(-lmbd)
          if x==np.inf:
              break
          decay.append(x)
          f=f+1
      
      print(decay)
      print("end")