From: Brandon McCombs on 5 Aug 2010 03:25 Hello, I'm building an elevator simulator for a class assignment. I recently ran into a roadblock and don't know how to fix it. For some reason, in my checkQueue function below, the call to self.goUp() is never executed. It is on the last line of code I pasted in. I can put print statements before and after the call and I have a print statement in goUp() itself. Only the print statements before and after the call are executed. The one inside goUp() is never executed because goUp() never seems to be executed. How can that be? I don't get any errors when the script executes. Surely this isn't some limitation I'm encountering? thanks sorry about the formatting --------------------------------------------- class Elevator(Process): def __init__(self,name): Process.__init__(self,name=name) self.numPassengers = 0 self.passengerList = [] self.passengerWaitQ = [] self.currentFloor = 1 self.idle = 1 self.newPassengers = 0 def goUp(self): print "here" bubbleSort(self.passengerList, len(self.passengerList)) self.currentFloor += 1 if len(self.passengerList) > 0: for p in self.passengerList: if self.currentFloor == p.destination: yield (p.destination - self.currenteFloor) * TRAVELTIME, self reactivate(p) p.inBuilding() else: self.goUp() def checkQueue(self): if (len(self.passengerWaitQ)) > 0 and len(self.passengerList) < MAXCAPACITY: if len(self.passengerWaitQ) < MAXCAPACITY: self.newPassengers = len(self.passengerWaitQ) else: self.newPassengers = MAXCAPACITY - len(self.passengerList) for i in range(0,self.newPassengers): self.passengerList.append(self.passengerWaitQ.pop()) self.goUp()
From: Navkirat Singh on 5 Aug 2010 03:36 I was looking at the code, I dont have much time to go through it, but I might have found a typo - yield (p.destination - self.currenteFloor) , I think it should be currentFloor.Maybe thats your problem. Will look into the code more later. Regards, Nav On 05-Aug-2010, at 12:55 PM, Brandon McCombs wrote: > Hello, > > I'm building an elevator simulator for a class assignment. I recently ran into a roadblock and don't know how to fix it. For some reason, in my checkQueue function below, the call to self.goUp() is never executed. It is on the last line of code I pasted in. I can put print statements before and after the call and I have a print statement in goUp() itself. Only the print statements before and after the call are executed. The one inside goUp() is never executed because goUp() never seems to be executed. How can that be? I don't get any errors when the script executes. Surely this isn't some limitation I'm encountering? > > thanks > > sorry about the formatting > > --------------------------------------------- > class Elevator(Process): > def __init__(self,name): > Process.__init__(self,name=name) > self.numPassengers = 0 > self.passengerList = [] > self.passengerWaitQ = [] > self.currentFloor = 1 > self.idle = 1 > self.newPassengers = 0 > def goUp(self): > print "here" > bubbleSort(self.passengerList, len(self.passengerList)) > self.currentFloor += 1 > if len(self.passengerList) > 0: > for p in self.passengerList: > if self.currentFloor == p.destination: > yield (p.destination - self.currenteFloor) * TRAVELTIME, self > reactivate(p) > p.inBuilding() > else: > self.goUp() > > def checkQueue(self): > if (len(self.passengerWaitQ)) > 0 and len(self.passengerList) < MAXCAPACITY: > if len(self.passengerWaitQ) < MAXCAPACITY: > self.newPassengers = len(self.passengerWaitQ) > else: > self.newPassengers = MAXCAPACITY - len(self.passengerList) > for i in range(0,self.newPassengers): > self.passengerList.append(self.passengerWaitQ.pop()) > self.goUp() > -- > http://mail.python.org/mailman/listinfo/python-list
From: Jon Clements on 5 Aug 2010 05:50 On 5 Aug, 08:25, Brandon McCombs <n...(a)none.com> wrote: > Hello, > > I'm building an elevator simulator for a class assignment. I recently > ran into a roadblock and don't know how to fix it. For some reason, in > my checkQueue function below, the call to self.goUp() is never executed. > It is on the last line of code I pasted in. I can put print statements > before and after the call and I have a print statement in goUp() itself. > Only the print statements before and after the call are executed. The > one inside goUp() is never executed because goUp() never seems to be > executed. How can that be? I don't get any errors when the script > executes. Surely this isn't some limitation I'm encountering? > > thanks > > sorry about the formatting > > --------------------------------------------- > class Elevator(Process): > def __init__(self,name): > Process.__init__(self,name=name) > self.numPassengers = 0 > self.passengerList = [] > self.passengerWaitQ = [] > self.currentFloor = 1 > self.idle = 1 > self.newPassengers = 0 > def goUp(self): > print "here" > bubbleSort(self.passengerList, len(self.passengerList)) > self.currentFloor += 1 > if len(self.passengerList) > 0: > for p in self.passengerList: > if self.currentFloor == p.destination: > yield (p.destination - self.currenteFloor) * TRAVELTIME, self > reactivate(p) > p.inBuilding() > else: > self.goUp() > > def checkQueue(self): > if (len(self.passengerWaitQ)) > 0 and len(self.passengerList) < > MAXCAPACITY: > if len(self.passengerWaitQ) < MAXCAPACITY: > self.newPassengers = len(self.passengerWaitQ) > else: > self.newPassengers = MAXCAPACITY - len(self.passengerList) > for i in range(0,self.newPassengers): > self.passengerList.append(self.passengerWaitQ.pop()) > self.goUp() Hi Brandon, Nice one at having a good crack at coding before posting! From your posted code, I'm struggling to see what's trying to be taught to you for this class assignment. As a note it'll be worth reading PEP 8 regarding naming conventions, because it looks very Java-ish to me! (I might be taking too much a real-world approach in the following, but do with it as you will...) I'm assuming that MAXCAPACITY and TRAVELTIME are globals somewhere. Although what I'm thinking is that different Elevators will have different capacities and different floors they service. An Elevator is *not* going to know its number of passengers (the most it could do is capacity based on weight restrictions) therefore it's not going to know the number of new passengers on each floor either. A couple of things that'd be worthwhile: 1) Post the requirements for your assignment - what's it supposed to output etc... 2) Go find an elevator, take a pen and pad with you, and stand in it for 30 mins or so, and see how the real thing deals with situations and make notes. ie, does it queue requests, or decide to take the next nearest floor, when does it stop and open etc...? hth Jon.
From: Ulrich Eckhardt on 5 Aug 2010 06:35 Brandon McCombs wrote: > I'm building an elevator simulator for a class assignment. I recently > ran into a roadblock and don't know how to fix it. For some reason, in > my checkQueue function below, the call to self.goUp() is never executed. [...] > sorry about the formatting While I can certainly forgive you the formatting (my problem is rather that you didn't reduce the code to the smallest possible example), Python wont. Python is a language where whitespace is significant and can subtly change the meaning of your code. Example: > for i in range(0,self.newPassengers): > self.passengerList.append(self.passengerWaitQ.pop()) > self.goUp() The formatting here is completely removed, but there are two conceivable ways this could be formatted: # variant 1 for i in range(0,self.newPassengers): self.passengerList.append(self.passengerWaitQ.pop()) self.goUp() #variant 2 for i in range(0,self.newPassengers): self.passengerList.append(self.passengerWaitQ.pop()) self.goUp() Someone already mentioned PEP 8 (search the web!). These PEPs could be called the standards governing Python behaviour, and PEP 8 actually defines several things concerning the formatting of sourcecode. Apply it unless you have a good reason not to. Further, you should run Python with "-t" as argument on the commandline. This will give you warnings when it encounters inconsistent tab/spaces usage. This can make a difference. Example: #variant 3 for i in range(0,self.newPassengers): self.passengerList.append(self.passengerWaitQ.pop()) <TAB>self.goUp() If your editor is set to four spaces per tab, this will look like variant 2, with 8 spaces it will look like variant 1. I don't know (and don't care, since PEP-8 mandates four spaces) which interpretation Python actually uses. Lastly, you can simplify your check_queue() function. First, determine the number of free places inside the elevator. Then, you simply append that many passengers from the waiting list to the passenger list: free = MAX_CAPACITY - len(self.passengers) new_passengers = self.passenger_wait_queue[:free] self.passenger_wait_queue = self.passenger_wait_queue[free:] self.passengers += new_passengers This uses the fact that list indices are automatically truncated to a valid range, so requesting the elements 0 to 10 from a 5-element list will only yield those five elements, not raise an exception. It's up to you though which version is clearer to you. I would perhaps bail out if "free == 0" and then also not call go_up() lateron. Cheers and good luck! Uli -- Sator Laser GmbH Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
From: Brandon McCombs on 5 Aug 2010 11:15 Jon Clements wrote: > On 5 Aug, 08:25, Brandon McCombs <n...(a)none.com> wrote: >> Hello, >> >> I'm building an elevator simulator for a class assignment. I recently >> ran into a roadblock and don't know how to fix it. For some reason, in >> my checkQueue function below, the call to self.goUp() is never executed. >> It is on the last line of code I pasted in. I can put print statements >> before and after the call and I have a print statement in goUp() itself. >> Only the print statements before and after the call are executed. The >> one inside goUp() is never executed because goUp() never seems to be >> executed. How can that be? I don't get any errors when the script >> executes. Surely this isn't some limitation I'm encountering? >> >> thanks >> >> sorry about the formatting >> >> --------------------------------------------- >> class Elevator(Process): >> def __init__(self,name): >> Process.__init__(self,name=name) >> self.numPassengers = 0 >> self.passengerList = [] >> self.passengerWaitQ = [] >> self.currentFloor = 1 >> self.idle = 1 >> self.newPassengers = 0 >> def goUp(self): >> print "here" >> bubbleSort(self.passengerList, len(self.passengerList)) >> self.currentFloor += 1 >> if len(self.passengerList) > 0: >> for p in self.passengerList: >> if self.currentFloor == p.destination: >> yield (p.destination - self.currenteFloor) * TRAVELTIME, self >> reactivate(p) >> p.inBuilding() >> else: >> self.goUp() >> >> def checkQueue(self): >> if (len(self.passengerWaitQ)) > 0 and len(self.passengerList) < >> MAXCAPACITY: >> if len(self.passengerWaitQ) < MAXCAPACITY: >> self.newPassengers = len(self.passengerWaitQ) >> else: >> self.newPassengers = MAXCAPACITY - len(self.passengerList) >> for i in range(0,self.newPassengers): >> self.passengerList.append(self.passengerWaitQ.pop()) >> self.goUp() > > Hi Brandon, > > Nice one at having a good crack at coding before posting! > > From your posted code, I'm struggling to see what's trying to be > taught to you for this class assignment. not relevant at this point > > As a note it'll be worth reading PEP 8 regarding naming conventions, > because it looks very Java-ish to me! ok but not relevant > > (I might be taking too much a real-world approach in the following, > but do with it as you will...) > > I'm assuming that MAXCAPACITY and TRAVELTIME are globals somewhere. > Although what I'm thinking is that different Elevators will have > different capacities and different floors they service. An Elevator is > *not* going to know its number of passengers (the most it could do is > capacity based on weight restrictions) therefore it's not going to > know the number of new passengers on each floor either. okay but not relevant to the problem at hand > > A couple of things that'd be worthwhile: > > 1) Post the requirements for your assignment - what's it supposed to > output etc... that isn't relevant for determining at the python level why a function simply isn't being called > 2) Go find an elevator, take a pen and pad with you, and stand in it > for 30 mins or so, and see how the real thing deals with situations > and make notes. ie, does it queue requests, or decide to take the next > nearest floor, when does it stop and open etc...? > > hth > > Jon. > actually it doesn't help at all since you decided to focus on everything but my actual question of why a function call wasn't working but rather question the validity of the program itself
|
Next
|
Last
Pages: 1 2 3 Prev: ██▓▒░░ *** CREDIT CARD *** ░▒░▒▓██ Next: subprocess escaping POpen?! |