-1
def plotx(arg):
    m=np.linspace(-np.pi,np.pi,1000)
    l=arg.split(",")
    l1=l[0]
    l1=l1.replace("?","m")
    l2=l[1]
    l2=l2.replace("?","m")
    print(l1)
    print(l2)
    exec("x ="+l1)
    exec("y ="+l2)
    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)
    ax.spines['left'].set_position('center')
    ax.spines['bottom'].set_position('center')
    ax.spines['right'].set_color('none')
    ax.spines['top'].set_color('none')
    ax.xaxis.set_ticks_position('bottom')
    ax.yaxis.set_ticks_position('left')
    plt.plot(x,y)
    plt.show()
  

@client.command()
async def plot(ctx,arg):
    plotx(arg)

Error Side

Traceback (most recent call last): File "D:\Users\Arda\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\bot.py", line 892, in invoke await ctx.command.invoke(ctx) File "D:\Users\Arda\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 797, in invoke await injected(*ctx.args, **ctx.kwargs) File "D:\Users\Arda\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 92, in wrapped raise CommandInvokeError(exc) from exc discord.ext.commands.errors.CommandInvokeError: Command raised an exception: NameError: name 'x' is not defined

Im getting this error how ı can fix it?

TrebledJ
  • 8,713
  • 7
  • 26
  • 48
  • Also I wrote this code in different file and it works – Blades of Pawel Dec 12 '20 at 19:35
  • What? exec("x ="+l1)? Did you mean x=l1? – quamrana Dec 12 '20 at 19:37
  • Does this answer your question? [Setting variables with exec inside a function](https://stackoverflow.com/questions/23168282/setting-variables-with-exec-inside-a-function) – TrebledJ Dec 12 '20 at 19:40
  • No .l1 is like np.sin (m). And exec ("x =" + l1) ==> x = np.sin (m) # np.sin(m) calculated with exec function – Blades of Pawel Dec 12 '20 at 19:41
  • 1
    According to [this](https://stackoverflow.com/questions/23168282/setting-variables-with-exec-inside-a-function), performing `exec("var = ...")` is highly not recommended. Simply doing `x = l1` as quamrana suggests, should fix the error. – TrebledJ Dec 12 '20 at 19:41
  • Hang on... at ("x=" + l1) `l1` may have an `'m'` in it, that can't be a value that would be valid to assign to `x`. – JeffUK Dec 12 '20 at 19:48
  • Ah, I guess since `l1` is a string, if it's something like "[1, 2, 3]", then you might want to use [`ast.literal_eval` or `eval`](https://stackoverflow.com/questions/15197673/using-pythons-eval-vs-ast-literal-eval) instead (prefer the former). – TrebledJ Dec 12 '20 at 19:54
  • Probably what I want.Thank you – Blades of Pawel Dec 12 '20 at 19:58

2 Answers2

0

It seems you're not assigning any value to the x and y variables that your code is calling in plt.plot(x, y).

olenscki
  • 487
  • 7
  • 22
0

You can replace

l=arg.split(",")
l1=l[0]
l1=l1.replace("?","m")
l2=l[1]
l2=l2.replace("?","m")
exec("x ="+l1)
exec("y ="+l2)

with

l=arg.split(",")
x=l[0].replace("?","m")
y=l[1].replace("?","m")

even clearer would be:

arg = arg.replace("?","m")
x,y = arg.split(",")

you could make it a one-liner but I think that sacrifices clarity.

Even better would be to just accept x and y as arguments in the first place!

JeffUK
  • 4,107
  • 2
  • 20
  • 34