我在制作读取列表中"vector id"的函数时遇到问题



所以,我的向量类代码是:

最小值:

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:nnt-> red (type `r`)nt-> green (type `g`)nt-> blue (type `b`)nt-> yellow (type `y`)nt-> 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)

现在,向量应该是这样的。我的问题是,我需要制作一个函数,读取列表中的每个id并更新它。我唯一的问题是识别id。我尝试了多种方法,得到了这里:

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

我使用这个功能:

def updateByID(VectorRepository):
if len(VectorRepository.getList())==0:
print("~~~~~~~~~~~~~nt 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:nnt-> red (type `r`)nt-> green (type `g`)nt-> blue (type `g`)nt-> yellow (type `y`)nt-> 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?nt>>> "))
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!")

因此,通过使用该函数,用户输入一个id,然后创建一个新的向量。如果id与已创建的id相同,则会用新创建的id覆盖该id。但同样,它无法识别保存的任何id。我没什么想法吗?

setName_id()的参数应该是一个整数,但v是一个向量。您需要用新向量替换列表的整个元素,而不仅仅是设置其名称。

class VectorRepository():
def __init__(self, l=None):
self.__vector_list = [] if l is None else l
def updateIDChoice(self,nameid,v):
for index, elem in enumerate(self.__vector_list): that contains every vector made
if elem.getName_id()==nameid:
self.__vector_list[index] = v
break
else:
raise ValueError(f"Element {nameid} not found")

我还修复了如何使用默认值初始化__vector_list,这样就不会在所有存储库中获得相同的列表。

嗯,经过一些尝试和错误,这次编辑使我的程序按预期工作!

def updateIDChoice(self,nameid,v):
index=0
for elem in (self.__vector_list):
if elem.getName_id()==nameid:
self.__vector_list[index]=v
break
index+=1
else:
raise ValueError("Element not found")

谢谢Barmar的帮助!

最新更新