这是我第一次在stackoverflow上发帖,非常感谢您的帮助。我对python比较陌生,这是我第一次从事涉及编码的个人项目。该文件的最终目的是创建一个类,以根据输入来绘制电动机曲线。我的主文件当前是:
import motor
import plot
if __name__ == '__main__':
try:
x = motor.motor(6.5,0.275,0.0343,1047,6)
print(x.Imax,x.Imin,x.Tmax,x.Wmax,x.V,x.Kv(),x.Kt())
a = map(1047,0.0343,x.kt,0.275,6)
#a.plot()
except KeyboardInterrupt:
print('Quitting')
motor.py正常运行。当代码到达= map(1047,0.343,x.kt,0.275,6)时,出现错误:TypeError:'float'对象不可迭代。 map是我创建的一个类,由plot.py提供。当前代码如下:
class map:
def __init__(self,Wmax,Tmax,kt,Imin,V):
#Defines Inputs
self.Wmax = Wmax
self.Tmax = Tmax
self.kt = kt
self.Imin = Imin
self.V = V
#Defines motor opperating range
self.Wrange = np.arange(0,int(self.Wmax),1)
#Defines Torque as a function of angular velocity
#self.T_function = self.Tmax*(1-self.Wrange/self.Wmax)
#Defines mechanical power as a functionof angular velocity
#self.Pmech_function = self.Tmax*(1-self.Wrange/self.Wmax)\
#*self.Wrange
#Defines electrical power as a function of Torque
#self.Pelec_function =((self.Tmax*(1-self.Wrange/self.Wmax\
#))/self.kt+self.Imin)*self.V
#Defines efficiency as a function of pmech and pelec
#self.eff_function = (self.Tmax*(1-self.Wrange/self.Wmax)*\
#self.Wrange)/(((self.Tmax*(1-self.Wrange/self.Wmax))/self\
#.kt+self.Imin)*self.V)
#self.Wlabel = 'Angular Velocity (rad/sec)'
#self.Tlabel = 'Torque (N-m)'
#self.Pmech_label = 'Mechanical Power (Watts)'
#self.Pelec_label = 'Electrical Power (Watts)'
#self.eff_label ='Efficiency (%)'
""" def plot(self):
#Creates main window
fig = plt.figure()
#Creates four y-axis
ax = fig.add_subplot(111)
ax2 = ax.twinx()
ax3 = ax.twinx()
ax4 = ax.twinx()
#Plots the above functions against Wrange
ax.plot(self.Wrange,self.T_function,'-',color='black')
ax1.plot(self.Wrange,self.Pmech_function,'-',color='red')
ax2.plot(self.Wrange,self.Pelec_function,'-',color='blue')
ax3.plot(self.Wrange,self.eff_function,'-',color='green')
#Defines labels and grid
ax4.grid(True)
ax.set_xlabel(self.Wlabel)
ax.set_ylabel(self.Tlabel)
ax1.set_ylabel(self.Pmech_label)
ax2.set_ylabel(self.Pelec_label)
ax3.set_ylabel(self.eff_label)
plt.show()
"""
test = map(1047,0.343,0.00551,0.275,6)
print(test.Wrange)
当我运行plot.py作为它自己的文件时,最终功能上的两条测试行按预期提供了没有错误。如何解决此错误?对于构建这样的程序有什么建议吗?
编辑:我不知道map是标准库中的函数。我将来会在寻找。我还将在以后的文章中减少多余的代码。谢谢。
答案 0 :(得分:5)
不要将您的课程称为“地图”。那是标准库中的函数名,所以事情会变得混乱。您的呼叫a = map(1047,0.343,x.kt,0.275,6)
正在获得标准库1,这就是错误消息怪异而令人困惑的原因。如果重命名该类,那么事情会更有意义。
按照惯例,您的类名称应以大写字母开头(部分是为了避免这种混淆)。