0

I want to get the first value for "Forest" but im getting an "o"

zones = {"Forest":[1,50]}

for area in zones:
    spawnChance = area[1]

print spawnChance

2 Answers2

1

Python dicts when used as iterables yield their keys. As a result, area is equal to "Forest" in your example.

Also, index of first element in array is zero, not one (I believe only Basic uses one-based lists/arrays).

To access both keys and values use items or, better, iteritems (consult with this question on SO for difference between them)

for key, value in zones.iteritems():
    spawnChance = value[0]

Also, if you don't really care about the key, you can just use values:

for area in zones.values():
    spawnChance = area[0]
Community
  • 1
  • 1
J0HN
  • 26,063
  • 5
  • 54
  • 85
1

If you want to keep the code, use dict[key] to get te value.

zones = {"Forest":[1,50]}

for area in zones:
    spawnChance = zones[area][1]

print spawnChance
rafaelc
  • 57,686
  • 15
  • 58
  • 82