so, my code for a vector class is this:
minimal:
class MyVector:
def __init__(self, vname_id="pizza", vcolour="pizza", vtype=4, vvalues=np.array([None])):
if not(isinstance(vname_id,str)):
raise AttributeError("You did not enter a word. Please do so next time :(")
self.__name_id = vname_id
if not(isinstance(vcolour,str)):
raise AttributeError("You did not enter a word. Please do so next time :(")
self.__colour = vcolour
if not(isinstance(vtype,int)):
raise AttributeError("You did not enter a number. Please do so next time :(")
self.__type = vtype
self.__values = vvalues
def setName_id(self, vname_id):
if not(isinstance(vname_id,int)):
raise AttributeError("You did not enter a number. Please do so next time :(")
self.__name_id = vname_id
def setColour(self, vcolour):
if not(isinstance(vcolour,str)):
raise AttributeError("You did not enter a word. Please do so next time :(")
check=0
ColourList=["r",'g','b','y','m']
for elem in ColourList:
if vcolour==elem:
self.__colour=vcolour
check=1
break
if check==0:
print("\nSorry, forgot to tell you that the colour has to be between:\n\n\t-> red (type `r`)\n\t-> green (type `g`)\n\t-> blue (type `b`)\n\t-> yellow (type `y`)\n\t-> magenta (type `m`)\nPlease try again.")
time.sleep(2)
print("I will show you the main menu in a moment.")
time.sleep(3)
self.__colour = vcolour
def setType(self, vtype):
self.__type = vtype
def setValues(self, listnumbers):
for elem in listnumbers:
if not(isinstance(elem,int)):
raise ValueError
self.__vvalues=np.array(listnumbers)
def GetValues(self):
return self.__values
def getName_id(self):
return self.__name_id
def getColour(self):
return self.__colour
def getType(self):
return self.__type
def __str__(self):
return "Vector with the ID " + str(self.__name_id) + ", of colour " + str(self.__colour) + " and made from type " + str(self.__type) + ", is " + str(self.__vvalues)
Now, that's how the vector is supposed to be. My question is that i need to make a function that reads every id in a list and updates it. My only problem is recognizing the id. I tried multiple ways and got here:
class VectorRepository():
def __init__(self, l=[]):
self.__vector_list=l
def updateIDChoice(self,nameid,v):
for index, elem in enumerate(self.__vector_list):#vector_list is the list that contains every vector made
if elem.getName_id()==nameid:#nameid is the id given by the user
self.__vector_list[index].setName_id(v)#v is the newly created vector
and im using this function:
def updateByID(VectorRepository):
if len(VectorRepository.getList())==0:
print("~~~~~~~~~~~~~\n\t The list appears to be empty! :( \n~~~~~~~~~~~~~")
else:
try:
v=MyVector()
values=[]
nameid=int(input("\tPlease enter the id: "))
choiceid=int(input("\tpls enter for id: "))
if choiceid < 0:
raise ValueError("Your number needs to be positive. Like Superman!")
v.setName_id(choiceid)
choicecolour=str(input("\tpls enter for colour: "))
check=0
ColourList=["r",'g','b','y','m']
for elem in ColourList:
if choicecolour==elem:
v.setColour(choicecolour)
check=1
break
if check==0:
raise ValueError("\nSorry, forgot to tell you that the colour has to be between:\n\n\t-> red (type `r`)\n\t-> green (type `g`)\n\t-> blue (type `g`)\n\t-> yellow (type `y`)\n\t-> magenta (type `m`)\nPlease try again in a moment.")
v.setColour(choicecolour)
choicetype=int(input("\tpls enter for type: "))
v.setType(choicetype)
if choicetype < 1:
raise ValueError("Your number needs to be 1 or greater!")
n=int(input("\tVectors can have many different values. How many values do you want this vector to have?\n\t>>> "))
for choice in range(0, n):
print("\tEnter the value for value",choice+1,": ")
choice = int(input("\t>>>"))
values.append(choice)
v.setValues(values)
VectorRepository.updateIDChoice(nameid,v)
except:
print("\tSorry, you did something wrong and i froze. Please read the rules and try again!")
So, by using the function, the user inputs a id, then creates a new vector. if the id is the same with one already made, it overwrites that one with the newly created one. But again, it doesn't recognize any id saved. Any ideas im missing?