python 未绑定本地错误:赋值前引用局部变量"y"

1tuwyuhd  于 2022-12-28  发布在  Python
关注(0)|答案(1)|浏览(123)
import matplotlib.pyplot as plt
import numpy as np

#F(x;y) = a · x + b · y
def F1(a, b):
    x = np.linspace(-6,9,100)
    y = a*x + b
    return x, y

#F(x; y) = a · x - b · y
def F2(a, b):
    x = np.linspace(-6,9,100)
    y = a*x - b
    return x, y

#F(x; y) = a · x · b · y
def F3(a, b):
    x = np.linspace(-6,9,100)
    y = a*x*b
    return x, y

#F(x; y) = (a · x) / (b · y)
def F4(a, b):
    x = np.linspace(-6,9,100)
    y = (a*x)/(b*y)
    return x, y

plt.title("Euler's method")
plt.xlabel("x")
plt.ylabel("y")

a = 5
b = 1

x, y = F1(a, b)
plt.plot(x, y, label="F1(x,y): a · x + b · y")

x, y = F2(a, b)
plt.plot(x, y, label="F2(x,y): a · x - b · y")

x, y = F3(a, b)
plt.plot(x, y, label="F3(x,y): a · x · b · y")

x, y = F4(a, b)
plt.plot(x, y, label="F4(x,y): (a · x) / (b · y)")

plt.legend()
plt.show()

我在Python中收到以下错误(我使用的是Jupyter):

UnboundLocalError: local variable 'y' referenced before assignment

in line 25 and 44

25 line y = (a*x)/(b*y)

44 line x, y = F4(a, b)

怎么解决呢?

f0brbegy

f0brbegy1#

在你的F4函数中,你需要改变你的方程来得到y值。。你不能在给y赋值之前让它在右边。

def F4(a, b):
    x = np.linspace(-6,9,100)
    y = (a*x)/(b)
    return x, y

相关问题