2017-07-17 71 views
0

我敢肯定这是一个非常简单的问题,但我很难修正对象列表中单数对象内的元素。我呼吁志愿者对象设置:如何修改列表中的对象中的元素?

#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() 
+0

除非你有一个共享状态,我没有看到,我看不到这个代码导致这种行为。 – Carcigenicate

+0

这段代码似乎完全失败了,因为你没有指定一个带参数的构造函数。这段代码不会运行,所以我不确定你如何测试它。 – Carcigenicate

+0

看起来问题可能与您的属性实例有关。尝试在类的'__init__'方法中设置属性。 –

回答

1

更改此:

class Volunteer(): 
    consecutiveDaysOff = 0 
    totalDaysOff = 0 
    locWorked = [] 
    schedule = [] 

这样:

class Volunteer(): 
    consecutiveDaysOff = 0 
    totalDaysOff = 0 
    def __init__(self): 
     self.locWorked = list() 
     self.schedule = list() 

你的代码是用列表的同一个实例在你的志愿对象的每个实例。

+0

这并没有解决它。 – TheSaint321

+0

对不起,我编辑了我的意思。 –

+0

非常感谢你! – TheSaint321

0

更改此

> class Volunteer(): 
>  consecutiveDaysOff = 0 
>  totalDaysOff = 0 
>  locWorked = [] 
>  schedule = [] 
> 
>  def newDayOff(self): 
>   self.totalDaysOff =+1 
>   self.consecutiveDaysOff =+ 1 
>   self.schedule.insert(0,0) 

> class Volunteer(): 
>  def __init__(self): 
>   self.consecutiveDaysOff = 0 
>   self.totalDaysOff = 0 
>   self.locWorked = [] 
>   self.schedule = [] 
> 
>  def newDayOff(self): 
>   self.totalDaysOff =+1 
>   self.consecutiveDaysOff =+ 1 
>   self.schedule.insert(0,0) 

这应该解决这个问题..