Re: new to python - trouble calling a function from another function



Ulrich Eckhardt wrote:
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

so I missed a few lines, so sue me.

), 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:

already aware. I reformatted tabs to reduce the line wrap so it was easier for readers to read it.


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

either one of those should still execute self.goUp(). I'm not getting anything though no matter where I place the function call.


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.

Yeah I already tried that using 'tabnanny' I think it was called to diagnose one function that I decided to create and the -t option gave me false information. I determined that I had a tab in front of the function name (just like many others) however the actual fix was to put in spaces until it lined up with all the other 'def' lines.


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.



so you made other recommendations but didn't address my original question unless I missed it somewhere.
.



Relevant Pages

  • Re: Not so simple question
    ... For some reason the "Current Directory" tab is no longer viewable, ... or use "Five Panel" mode. ... why and how to fix it? ...
    (comp.soft-sys.matlab)
  • Not so simple question
    ... For some reason the "Current Directory" tab is no longer viewable, ... or use "Five Panel" mode. ... why and how to fix it? ...
    (comp.soft-sys.matlab)
  • tab and outlining
    ... For some reason, word no longer automatically promotes to ... the next outline level when I am in my outline and press ... tab (I have to go to the indent icon at the top of the ... Does anyone know how to fix this? ...
    (microsoft.public.word.numbering)
  • Re: Does Python really follow its philosophy of "Readability counts"?
    ... We're told Python doesn't have private attributes. ... the advice to not use getters/setters for no good reason, given Python's support for computed attributes - which is not exactly the same thing. ... stricter, with enforced data hiding, the objections come thick and fast: people vehemently say that they like Python just the way it is, that they want the ability to mess with the internals. ...
    (comp.lang.python)
  • Re: Sen. Specter threatens to cut off funding for secret wiretaps
    ... Of course that's not Iraq either but you get my drift. ... the same reason Clinton wasn't convicted by the senate. ... fix it is to legislatively close that loophole. ... initiating impeachment proceedings would be political suicide. ...
    (rec.martial-arts)