我敢肯定这是一个非常简单的问题,但我很难修正对象列表中单数对象内的元素。我呼吁志愿者对象设置:如何修改列表中的对象中的元素?
#Create the volunteer class
class Volunteer():
consecutiveDaysOff = 0
totalDaysOff = 0
locWorked = []
schedule = []
def __init__(self, employeeId):
self.employeeId = employeeId
def updateSchedule(self,location):
self.schedule.append(location)
def newDayOff(self):
self.totalDaysOff =+1
self.consecutiveDaysOff =+ 1
self.schedule.insert(0,0)
而且我有志愿者名单设置:
v1 = Volunteer(1)
v2 = Volunteer(2)
v3 = Volunteer(3)
volunteerList = [v1,v2,v3]
我想用列表的一个元素上运行newDayOff():志愿者列表[0] .newDayOff()
但每次我都会在列表中的每个Volunteer实例上运行newdayoff()。什么是正确的方法呢?
import csv
#Create the volunteer class
class Volunteer():
consecutiveDaysOff = 0
totalDaysOff = 0
locWorked = []
schedule = []
def __init__(self, employeeId):
self.employeeId = employeeId
def updateSchedule(self,location):
self.schedule.append(location)
def newDayOff(self):
self.totalDaysOff =+1
self.consecutiveDaysOff =+ 1
self.schedule.insert(0,0)
# Test Variables
v1 = Volunteer(1)
v2 = Volunteer(2)
v3 = Volunteer(3)
v2.consecutiveDaysOff = 1
v1.totalDaysOff = 1
v2.totalDaysOff = 3
v3.totalDaysOff = 4
numVolunteers = 3
location = [1]
numLocation = 1
volunteerList = [v1,v2,v3]
#test inputs
#Take inputs
# try:
# days = int(input("How many days are in the month? "))
# except ValueError:
# print("Invalid Input... Please Restart Program")
# exit()
#
# try:
# numVolunteers = input("How many volunteers are there? ")
# except ValueError:
# print("Invalid Input... Please Restart Program")
# exit()
# try:
# location = input("What are the locations? (Should be numbers separated by ',')")
#
# if location == "":
# print("Locations were not entered.")
# exit()
#
# numLocation = 1
# for i in range(0,len(location)):
# if location[i] == ',':
# numLocation = numLocation + 1
#
# location = location.replace(" ", "")
# location = location.split(',')
# location = list(map(int, location))
#
# except ValueError:
# print("Invalid Input... Please Restart Program")
# exit()
#create the volunteer list
def createVolunteerList():
for i in range(0,numVolunteers):
volunteerList.append(Volunteer(i))
def sortArrayByConsecDaysOff():
for i in range(0,len(volunteerList)):
for j in range(0,len(volunteerList)):
if volunteerList[i].consecutiveDaysOff > volunteerList[j].consecutiveDaysOff:
volunteerList[i],volunteerList[j] = volunteerList[j],volunteerList[i]
def sortArrayByTotalDaysOff():
for i in range(0,len(volunteerList)):
for j in range(0,len(volunteerList)):
if volunteerList[i].totalDaysOff < volunteerList[j].totalDaysOff:
volunteerList[i], volunteerList[j] = volunteerList[j], volunteerList[i]
def printVolunteer():
for i in range(0,len(volunteerList)):
v = volunteerList[i]
print(v.employeeId, " " ,v.consecutiveDaysOff, " " ,v.totalDaysOff, " " , v.locWorked, " ", v.schedule, "\n")
def assignDaysOff():
# Define number of days off that can be taken that day
remainingDaysOff = numVolunteers - numLocation
# Loop through volunteers to give weekends to those that have had one consecutive day off.
for i in range(0, len(volunteerList)):
if volunteerList[i].consecutiveDaysOff == 1 and remainingDaysOff > 0:
volunteerList[i].newDayOff()
remainingDaysOff = remainingDaysOff - 1
sortArrayByTotalDaysOff()
# Loop through volunteers to assign weekends to those with the fewest days off so far.
for i in range(0, len(volunteerList)):
if remainingDaysOff > 0 and not volunteerList[i]:
volunteerList[i].newDayOff()
remainingDaysOff = remainingDaysOff - 1
printVolunteer()
volunteerList[0].newDayOff()
printVolunteer()
除非你有一个共享状态,我没有看到,我看不到这个代码导致这种行为。 – Carcigenicate
这段代码似乎完全失败了,因为你没有指定一个带参数的构造函数。这段代码不会运行,所以我不确定你如何测试它。 – Carcigenicate
看起来问题可能与您的属性实例有关。尝试在类的'__init__'方法中设置属性。 –