1
I am creating these two methods in which the first worth the existence of a vehicle from your name. In the second, it is intended to add a new vehicle, validating first, if there is a vehicle with a given name, to avoid duplication of results. However, I always get the error described.
What can I do to prevent that from happening? Just the attribute preco_base
is mutable
def exists_viatura(self, nome):
for v in self.gestor:
if v.nome == nome:
return True
else:
print("Não existe nenhuma viatura")
def add_viatura(self, new_v):
for v in self.gestor:
new_v = Viatura(new_v.nome, new_v.modelo, new_v.tipo_electrica, new_v.preco_base)
if g.exists_viatura(new_v.nome):
print("Já existe uma viatura com esse nome")
self.gestor.append(new_v)
Error:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "C:/Users/sandr/PycharmProjects/projecto/Classes_Projecto.py", line 189, in add_viatura
new_v = Viatura(new_v.nome, new_v.modelo, new_v.tipo_electrica, new_v.preco_base)
AttributeError: 'str' object has no attribute 'nome'
What it says in the error is that the parameter new_v is a string and therefore has no attributes.
– tomasantunes
Okay, and how can I refer to the object Car and have it entered on the list?
– Sandra Silva
I think to see if it already exists just do so:
existe = new_v in self.gestor
, this will return False if there is no vehicle in the listgestor
– Natan Fernandes