I would like to make a function that can take a single element or a list as arguments.
def Density(m, *vol):
"""
Inputs:
m is the mass.
vol is the volume.
Output:
rho is the density.
"""
for _ in vol:
if isinstance(vol, float):
rho = m / _
elif isinstance(vol, (list,)):
rho = m / _ * 1000
else:
raise ValueError('It is neither a float nor a list, apparently!')
return rho
So, if I define a float and a list
flt = 0.25
lst = [0.25, 0.26, 0.24]
and try to pass it through Density
a = Density(50, flt)
b = [Density(50, _) for _ in lst]
the error I created appears.
- What am I doing wrong?