通过 parameters/variables 不正确
Passed parameters/variables aren't correct
在我的第二个 class 中使用继承练习 OOP 时,我在添加参数时遇到了问题,如您所见,我有:(self, name, tred, PM, PM2, ra, ra2)
。但是当 运行 程序告诉我不能乘法 Nonetype
时,我打印它们看看有什么问题,我发现一些参数不正确。
第一个 class 工作正常(Atomic_d
),我只需要了解它如何工作第二个 class 中的变量:Molecular_d
我该如何解决这个问题?
This is the complete code picture
import math
class Atomic_d():
def __init__(self,name="",red="",PM=None,ra=None):
self.name = name
self.red = red
self.PM = PM
self.ra = ra
def density(self):
redes = {"BCC":2,"FCC":4,"HCP":6}
self.red = self.red.upper()
nred = redes.get(self.red)
nav = round(6.022*(10**23),3)
if nred == 2:
a = (4*(self.ra))/((3)**0.5)
elif nred == 4:
a = (4*(self.ra))/((2)**0.5)
else:
per = round(12 * self.ra,3)
hipoc = round((2*self.ra)**2,3)
basec = round(self.ra**2,3)
apo = round(math.sqrt(hipoc - basec),3)
a = round((per * apo / 2),3)
if nred == 2 or nred == 4:
vol = a**3
elif nred == 6:
vol = round(a * 3.266 * self.ra,3)
density = round((((nred*self.PM)/(nav*vol))*10**21),3)
return "{} : {} g/cc".format(self.name,density)
class Molecular_d(Atomic_d):
def __init__(self,name="",tred="",PM=None,ra=None,PM2=None,ra2=None):
super().__init__(name,PM,ra)
self.PM2 = PM2
self.ra2 = ra2
self.tred = tred
def density(self):
tredes = {"NACL":8}
self.tred = self.tred.upper()
nred = tredes.get(self.tred)
nav = round(6.022*(10**23),3)
if nred == 8:
l = round(((2*self.ra)*(2*self.ra2)),3)
vol = l**3
density = ((8*(self.PM + self.PM2))/(nav*vol))
return "{}: {} g/cc".format(self.name,density)
This is the error that happens when I try to debug
我打印每个变量以查看问题所在
print("Hi I'm name",self.name)
print("Hi I'm ra",self.ra)
print("Hi I'm ra2",self.ra2)
print("Hi I'm PM",self.PM)
print("Hi I'm PM2",self.PM2)
Hi I'm name Ox de magnesio
Hi I'm ra None
Hi I'm ra2 0.14
Hi I'm PM 16.0
Hi I'm PM2 0.072
回溯:
TypeError Traceback (most recent call last)
<ipython-input-21-bb7a69690a34> in <module>
1 magnesium_ox = Molecular_d("Ox de magnesio","NaCl",24.31,16.0,0.072,0.14)
2
----> 3 magnesium_ox.density()
<ipython-input-19-064f60b8cfce> in density(self)
52
53 if nred == 8:
---> 54 l = round(((2*self.ra)*(2*self.ra2)),3)
55 vol = l**3
56
TypeError: unsupported operand type(s) for *: 'int' and 'NoneType'
当您调用 super()_init() 时,您省略了 red
参数。 Python 假设您传递的三个参数是 name
、red
和 pa
,尽管这似乎不是您想要的。
当您没有将 parameter= 附加到参数时,Python 假定您是从左到右给它们的。你需要写:
super().__init__(name=name, PM=PM, ra=ra)
如果您不打算将它们按与 class 相同的顺序放置,则需要使用关键字参数。尝试将 Molecular_d
更改为以下内容:
class Molecular_d(Atomic_d):
def __init__(self,name="",tred="",PM=None,ra=None,PM2=None,ra2=None):
super().__init__(name=name,PM=PM,ra=ra)
self.PM2 = PM2
self.ra2 = ra2
self.tred = tred
def density(self):
tredes = {"NACL":8}
self.tred = self.tred.upper()
nred = tredes.get(self.tred)
nav = round(6.022*(10**23),3)
if nred == 8:
l = round(((2*self.ra)*(2*self.ra2)),3)
vol = l**3
else:
vol = 10
density = ((8*(self.PM + self.PM2))/(nav*vol))
return "{}: {} g/cc".format(self.name,density)
mag = Molecular_d(name="a", tred="as", PM=1.1, ra=0.2, PM2=2.3, ra2=40.1)
result = mag.density()
print(result)
<< 'a: 4.5167718365991366e-24 g/cc'
我添加了默认值 vol,因为我不确定
你有:
class Atomic_d():
def __init__(self,name="",red="",PM=None,ra=None):
但是,初始化是:
class Molecular_d(Atomic_d):
def __init__(self,name="",tred="",PM=None,ra=None,PM2=None,ra2=None):
super().__init__(name,PM,ra)
它:
super().__init__(name,PM,ra)
前往:
def __init__(self,name="",red="",PM=None,ra=None):
作为
__init__(self,name=name,red=PM,PM=ra,ra=None):
因此,ra
参数未初始化。
在我的第二个 class 中使用继承练习 OOP 时,我在添加参数时遇到了问题,如您所见,我有:(self, name, tred, PM, PM2, ra, ra2)
。但是当 运行 程序告诉我不能乘法 Nonetype
时,我打印它们看看有什么问题,我发现一些参数不正确。
第一个 class 工作正常(Atomic_d
),我只需要了解它如何工作第二个 class 中的变量:Molecular_d
我该如何解决这个问题?
This is the complete code picture
import math
class Atomic_d():
def __init__(self,name="",red="",PM=None,ra=None):
self.name = name
self.red = red
self.PM = PM
self.ra = ra
def density(self):
redes = {"BCC":2,"FCC":4,"HCP":6}
self.red = self.red.upper()
nred = redes.get(self.red)
nav = round(6.022*(10**23),3)
if nred == 2:
a = (4*(self.ra))/((3)**0.5)
elif nred == 4:
a = (4*(self.ra))/((2)**0.5)
else:
per = round(12 * self.ra,3)
hipoc = round((2*self.ra)**2,3)
basec = round(self.ra**2,3)
apo = round(math.sqrt(hipoc - basec),3)
a = round((per * apo / 2),3)
if nred == 2 or nred == 4:
vol = a**3
elif nred == 6:
vol = round(a * 3.266 * self.ra,3)
density = round((((nred*self.PM)/(nav*vol))*10**21),3)
return "{} : {} g/cc".format(self.name,density)
class Molecular_d(Atomic_d):
def __init__(self,name="",tred="",PM=None,ra=None,PM2=None,ra2=None):
super().__init__(name,PM,ra)
self.PM2 = PM2
self.ra2 = ra2
self.tred = tred
def density(self):
tredes = {"NACL":8}
self.tred = self.tred.upper()
nred = tredes.get(self.tred)
nav = round(6.022*(10**23),3)
if nred == 8:
l = round(((2*self.ra)*(2*self.ra2)),3)
vol = l**3
density = ((8*(self.PM + self.PM2))/(nav*vol))
return "{}: {} g/cc".format(self.name,density)
This is the error that happens when I try to debug 我打印每个变量以查看问题所在
print("Hi I'm name",self.name)
print("Hi I'm ra",self.ra)
print("Hi I'm ra2",self.ra2)
print("Hi I'm PM",self.PM)
print("Hi I'm PM2",self.PM2)
Hi I'm name Ox de magnesio
Hi I'm ra None
Hi I'm ra2 0.14
Hi I'm PM 16.0
Hi I'm PM2 0.072
回溯:
TypeError Traceback (most recent call last)
<ipython-input-21-bb7a69690a34> in <module>
1 magnesium_ox = Molecular_d("Ox de magnesio","NaCl",24.31,16.0,0.072,0.14)
2
----> 3 magnesium_ox.density()
<ipython-input-19-064f60b8cfce> in density(self)
52
53 if nred == 8:
---> 54 l = round(((2*self.ra)*(2*self.ra2)),3)
55 vol = l**3
56
TypeError: unsupported operand type(s) for *: 'int' and 'NoneType'
当您调用 super()_init() 时,您省略了 red
参数。 Python 假设您传递的三个参数是 name
、red
和 pa
,尽管这似乎不是您想要的。
当您没有将 parameter= 附加到参数时,Python 假定您是从左到右给它们的。你需要写:
super().__init__(name=name, PM=PM, ra=ra)
如果您不打算将它们按与 class 相同的顺序放置,则需要使用关键字参数。尝试将 Molecular_d
更改为以下内容:
class Molecular_d(Atomic_d):
def __init__(self,name="",tred="",PM=None,ra=None,PM2=None,ra2=None):
super().__init__(name=name,PM=PM,ra=ra)
self.PM2 = PM2
self.ra2 = ra2
self.tred = tred
def density(self):
tredes = {"NACL":8}
self.tred = self.tred.upper()
nred = tredes.get(self.tred)
nav = round(6.022*(10**23),3)
if nred == 8:
l = round(((2*self.ra)*(2*self.ra2)),3)
vol = l**3
else:
vol = 10
density = ((8*(self.PM + self.PM2))/(nav*vol))
return "{}: {} g/cc".format(self.name,density)
mag = Molecular_d(name="a", tred="as", PM=1.1, ra=0.2, PM2=2.3, ra2=40.1)
result = mag.density()
print(result)
<< 'a: 4.5167718365991366e-24 g/cc'
我添加了默认值 vol,因为我不确定
你有:
class Atomic_d():
def __init__(self,name="",red="",PM=None,ra=None):
但是,初始化是:
class Molecular_d(Atomic_d):
def __init__(self,name="",tred="",PM=None,ra=None,PM2=None,ra2=None):
super().__init__(name,PM,ra)
它:
super().__init__(name,PM,ra)
前往:
def __init__(self,name="",red="",PM=None,ra=None):
作为
__init__(self,name=name,red=PM,PM=ra,ra=None):
因此,ra
参数未初始化。