[英]local variable referenced before assignment when trying to plot
I have the following code我有以下代码
from math import floor
import matplotlib.pyplot as plt
import numpy as np
def drive(n, n2all, sofar):
assert n > 0
if n == 1:
result = n2all[1]
else:
result = set()
push = result.add
for i in range(1, n//2 + 1):
for x in n2all[i]:
for y in n2all[n-i]:
for z in x+y, x*y:
if z not in sofar:
push(z)
return result
def e(n):
n2all = {1: set([1])}
sofar = set([1])
for i in range(1, n):
this = drive(i, n2all, sofar)
n2all[i] = this
sofar |= this
return min(list(this))
def E(n):
k = floor(n / 3)
if n ==1:
return 1
if n % 3 == 0:
return 3 ** k
if n % 3 == 1:
return 4 * 3**(k-1)
if n % 3 == 2:
return 2 * 3**k
def func(n):
return e(n) / E(n)
and I want to get a picture of distribution of func(n).我想了解 func(n) 的分布情况。 But whenever I try to calculate a values of func但是每当我尝试计算 func 的值时
n = 30
values = [func(i) for i in range(1, n)]
I get the following error我收到以下错误
local variable 'this' referenced before assignment赋值前引用的局部变量“this”
Or are there any other way to plot a hist or a line of this function?或者有没有其他方法来 plot 一个历史或这个 function 的一行?
I think the problem lies in the e()
function.我认为问题在于e()
function。 The for loop need to be like so:: for 循环需要像这样::
def e(n):
n2all = {1: set([1])}
sofar = set([1])
for i in range(1, n+1): #< ---just change this
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.