[Tutor] What's in a name?
Danny Yoo
dyoo at hashcollision.org
Fri Jan 3 10:34:26 CET 2014
> it works fine, but if I run it after I've loaded it (by candl(100) at the
> prompt, for example), it is way off: something isn't being reset, and I'm
> sure it'd be obvious to someone,
It's due to global variable mutation. Note that summarize_game()
mutates game_nums, which is either chutes or ladders. Basically, it
writes all over the structure of the game board, which means
subsequent play throughs are a wash. :P
And you know this, from your comment:
> Yes, game_array was left over, sorry. But chutes and ladders are
> dictionaries that should remain unchanged, for the purpose of
> future games.
But to be fair, this problem was also present in the original version
of the code, before my meddling.
This is relatively straightforward to fix: just modify summarize_game
so it creates a fresh dictionary mimicking the key structure of the
entry points. That way, it doesn't mess with chutes and ladders.
##########################################################
def summarize_game(game_name, game_map, game_index, garray):
game_nums = {}
for i in game_map.keys(): game_nums[i] = 0
for corl in game_nums:
for game in garray:
if corl in game[game_index]:
game_nums[corl] += 1
print("total ", game_name, "= ", sum(game_nums.values()))
##########################################################
In the context of the full program, I now understand why you have "4"
and "5" as mysterious constants now. Good. It's due to the structure
of the return value of games.play_game().
#################################################
return [step, self.move_count, self.num_chutes, self.num_ladders,
self.chutes_list, self.ladders_list]
#################################################
That is, this awkwardness of indexing by number to get at chutes_list
and ladders_list is due to that result wanting not to be in a
homogenous list or sequence. The return value there *really* wants to
be something more like a dictionary or object instead.
Don't have time at the moment to sketch more than that; must sleep so
I can work tomorrow. :P
More information about the Tutor
mailing list