python 2.7 : L += branch_length results in NameError: name 'L' not defined

标签 python python-2.7 nameerror

这是一个由我的教授分发并由学生修改的群体遗传学程序。

基本上,它应该使用给定的样本、人口和突变率 (u) 来模拟预期的突变次数 20 次。然而,一个关键部分是总分支长度 (L),它是各种较小分支长度 (branch_length) 的总和。但是,当我如下定义 L 时,它会不断返回错误:

  L += branch_length  
NameError: name 'L' is not defined

我不确定哪里出了问题,因为 tree_depth 的定义方式相同并且可以完美运行。

完整代码如下:

from random import expovariate
from pgen import poidev
K = 77       # sample size (number of gene copies)
twoN = 5000  # population size
u = .001

tree_depth = 0.0 # age of last common ancestor in generations

# Each pass through loop deals with one coalescent interval.

for A in range(20):
    while K > 1:
        h = K*(K-1)/(2.0*twoN) # hazard of a coalescent event
        t = expovariate(h)       # time until next coalescent event
        tree_depth += t
        branch_length = t*K
        K -= 1
        L += branch_length
    S = poidev(u*L)
    print "Simulation:", A+1, "Total Mutations:", S
print "Total tree depth (L):", L, "generations"

我是不是错过了一些非常非常明显的东西?提前致谢。

最佳答案

L += x 将 x 添加到现有的 L,但您尚未初始化 L。据推测,您希望 L = 0 位于文件顶部的某个位置.

关于 python 2.7 : L += branch_length results in NameError: name 'L' not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7693273/

相关文章:

python - 捕获 NameError 和错误处理

python - 在 Python 中,是否可以解压缩字符串列表并将每个字符串中的单个字符放入生成器中?

python - 如何迭代 pandas DataFrame 的列并从另一列返回值?

python - 正则表达式从字符串中获取数字

python - 使用 Python 和 SQL 时出现 NamError

ruby - 未初始化的常量生成器 (NameError) - Ruby

python - Pandas HDFStore.create_table_index 没有提高选择查询速度,寻找更好的搜索方式

Python - 根据值绘制彩色网格

python - 解析和聚合 Google Analytics 浏览器版本 CSV 数据

python - 使用 SSH 在 shell 中启动 Python 脚本