I’m trying to learn Python with the help of Learning Python the Hard Way.
I’ve reached exercise 41 (Gothons from Planet Percal #25), you can see the full code >here<
I understand everything until the last function runner()
def runner(map, start)
next = start
while True:
room = map[next]
print "\n--------"
next = room()
runner(ROOMS, 'central_corridor')
As far as I can understand, next is assigned the value of start, being the key of the first function to run. The the while loop is initiated assigning the function at that key to room.
Then the function prints out a line of dashes and after that it assigns the returned value of the function call to the variable next.
What I don’t understand is why the user “sees” the function being called. To me it looks like the function call is just assigned to a variable next. I would expect something like next() or room() being the next line. Secondly I don’t understand why the while-loop stops, shouldn’t it just continue until false or quit?
These might seem like foolish questions to most of you, but I’m new to the programming game and I don’t understand the answers being given to this question elsewhere on this site.
Hope someone can dumb down to my level and explain it to me…
The user sees the function being called, because the function prints things.
The function is actually being called (with “room()”) and the result of the call is set to next.
E.g. If the room is “the_bridge”, some stuff is printed and then “death”, “escape_pod” or “the_bridge” is returned.
While it’s true that “while True:” is an infinite loop, Python has a way to quit the program entirely.
The call “exit(0)” quits the whole program right there and then, no questions asked.