python - NameError: x is not defined -


मैं एक साधारण साजिश फलक plot2d ,

  def plot2d (xmin, xmax, func): x = np.linspace (xmin, xmax, num = 50) plt.plot (x, func) plt.show ()   <पी > विचार आप एक्स के मामले में चर 'func' इनपुट हैं, जैसे कि x ** 2।  

संपादित करें * यहां त्रुटि है:

  & gt; & gt; & gt ; plot2d (-10,10, x ** 2) ट्रेसबैक (सबसे हाल का कॉल अंतिम): फ़ाइल "& lt; stdin & gt;", लाइन 1, & lt; मॉड्यूल & gt; नाम: त्रुटि: नाम 'एक्स' परिभाषित नहीं है   

संपादित करें ** मुझे लगता है कि यह समस्या यह थी कि जब आप पहली बार फ़ंक्शन कॉल करते हैं, तो अब linspace x अभी तक नहीं बनाया गया है। यह काम किया:

  आयात के रूप में एनएमपी आयात matplotlib.pyplot plt def plot2d के रूप में (xmin, xmax): x = np.linspace (xmin, xmax, num = 50) func = input (' परिभाषित करें fucntion: ') plt.plot (x, func) plt.show ()    

<पी > आप इस बारे में सीखना चाह सकते हैं अपने कोड को थोड़ा सा पर्याप्त होगा:
  आयात के रूप में एनपी आयात matplotlib.pyplot plt डेफ plot2d (xmin, xmax, func) के रूप में: x = np.linspace (xmin, xmax, num = 50) plt.plot (x, func (x)) #func - & gt; Func (x) plt.show () #pass परम के रूप में एक अनाम लैम्ब्डा: plot2d (-10, 10, lambda x: x * x)    

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -