TypeError: Need an object - Frozen Fortress (Python)


my code raises TypeError: Need an object on line number 47:
while (mana < (mana_needed + mana_buffer)):

All my code here:

# CodeCombat Level: "Frozen Fortress"

# Initiate variables
upgrade_round = 0
positions = ["a", "b", "c", "d", "e", "f", "g", "h"]
level = {"a": 0, "b": 0, "c": 0, "d": 0, "e": 0, "f": 0, "g": 0, "h": 0}
tower_price = 10
mana_buffer = 10
min_dist_to_enemy = 13

# Set algorithm attributes
priority = {"a": 1, "b": 1,
            "c": 1, "d": 1,
            "e": 1, "f": 1,
            "g": 1, "h": 1}

tower_types = {"a": "farm",
               "b": "farm",
               "c": "farm",
               "d": "farm",
               "e": "farm",
               "f": "farm",
               "g": "farm",
               "h": "barracks"}

# Define functions
def defense_enemies():
    enemy = hero.findNearestEnemy()
    if (enemy and enemy.distanceTo(hero) < min_dist_to_enemy):
        hero.ability("shockwave", enemy)

def tower_chnage_type(pos, tower_type, priority):
    tower_types[pos] = tower_type
    priority[pos] = priority
    level[pos] = 0

# Main loop
while True:
    mana = hero.mana
    for pos in positions:
        mana_needed = level[pos] * tower_price * priority[pos]
        while (mana < (mana_needed + mana_buffer)):  # <-- Error indicated here
            mana = hero.mana
        for _i in priority[pos]:
            hero.build(tower_types[pos], pos)
            level[pos] += 1
    mana_buffer = 100 * upgrade_round
    upgrade_round += 1

What kind of error should I look for? Do I miss something?

Thanks for help

The problem is in this line for _i in priority[pos]: – you are trying to iterate a number.
I will report a bug with the wrong shown error. Thank you for the report.

1 Like

thank you!
range() fixed it :slight_smile:

Don’t you know why in the (fixed) loop

        for _i in range(priority[pos]):
            hero.build(tower_types[pos], pos)
            level[pos] += 1

for higher priority, let’s say 3, it doesn’t build up (upgrade) the tower +3 levels but only one?

What happens when I hero.build() but do not have enough mana for it? Does it wait until I earn mana, build/upgrade the tower and continue or skip the building line and continue?

Thank you for your hints.