# Level: Mad Maxer Sells Out - Problem{SOLVED}

**URL:** <https://discourse.codecombat.com/t/level-mad-maxer-sells-out-problem-solved/3660>\
**Category:** Level Help\
**Created:** [April 24, 2015, 1:31am UTC](https://discourse.codecombat.com/t/level-mad-maxer-sells-out-problem-solved/3660 "2015-04-24T01:31:38Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eowyn](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/eowyn/32/2494_2.png) [@Eowyn](https://discourse.codecombat.com/u/Eowyn)\
**Post date:** [April 24, 2015, 1:31am UTC](https://discourse.codecombat.com/t/level-mad-maxer-sells-out-problem-solved/3660/1 "2015-04-24T01:31:38Z")

</div>

I followed all the directions, but my hero is not collecting the nearest coin. He’s taking extra time to go to a coin farther away, and then he can’t get all the gold coins before they disappear.

I think it is because the variable `minGoldDist` starts out so high, and so the first coin is kind of random.

Here is a copy of my code. I have a similar problem in the next level, _Mad Maxer Gets Greedy_. Help would be greatly appreciated!!

```
# Coins here disappear after a few seconds!
# Get all the gold coins before they vanish.

loop:
    closestGold = None
    minGoldDist = 9000
    coinIndex = 0
    coins = self.findItems()
    # Find the closest coin that is gold.
    # Remember that gold coins have a value of 3.
    while coinIndex < len(coins):
        coin = coins[coinIndex]
        distance = self.distanceTo(coin)
        if distance < minGoldDist:
            if coin.value == 3:
                closestGold = coin
        coinIndex += 1
        
    if closestGold:
        self.moveXY(closestGold.pos.x, closestGold.pos.y)
```

---

<div class="post-metadata">

**Author:** ![Vlevo](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/vlevo/32/2074_2.png) [@Vlevo](https://discourse.codecombat.com/u/Vlevo)\
**Post date:** [April 24, 2015, 3:14am UTC](https://discourse.codecombat.com/t/level-mad-maxer-sells-out-problem-solved/3660/2 "2015-04-24T03:14:07Z")

</div>

You never sent minGoldDist to the dist of the closetGold so it will always be the last gold coin checked…

---

<div class="post-metadata">

**Author:** ![Eowyn](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/eowyn/32/2494_2.png) [@Eowyn](https://discourse.codecombat.com/u/Eowyn)\
**Post date:** [April 25, 2015, 4:16pm UTC](https://discourse.codecombat.com/t/level-mad-maxer-sells-out-problem-solved/3660/3 "2015-04-25T16:16:04Z")

</div>

Thanks! I think that did the trick. I just added this line of code:

```python
minGoldDist = self.distanceTo(closestGold)

```

I guess I just didn’t think about the fact that _minGoldDist_ was never getting reset!

---

<div class="post-metadata">

**Author:** ![Vlevo](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/vlevo/32/2074_2.png) [@Vlevo](https://discourse.codecombat.com/u/Vlevo)\
**Post date:** [April 25, 2015, 8:31pm UTC](https://discourse.codecombat.com/t/level-mad-maxer-sells-out-problem-solved/3660/4 "2015-04-25T20:31:28Z")

</div>

Actually, this is the “distance” I had this in mind…

```python
        if distance < minGoldDist:
            if coin.value == 3:
                closestGold = coin

```

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; minGoldDist = **distance**

since you already know “distance” to the current coin, no need to calculate it again. 😄

---

<div class="post-metadata">

**Author:** ![CodeExpert](https://avatars.discourse-cdn.com/v4/letter/c/a183cd/32.png) [@CodeExpert](https://discourse.codecombat.com/u/CodeExpert)\
**Post date:** [February 8, 2016, 3:54pm UTC](https://discourse.codecombat.com/t/level-mad-maxer-sells-out-problem-solved/3660/5 "2016-02-08T15:54:22Z")

</div>

Sorry, but where exactly in the code should I put the “minGoldDist = distance”?

It goes right after the “if distance \< minGoldDist” but before the “if coin.value == 3”, right?

---

<div class="post-metadata">

**Author:** ![bennypr0fane](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/bennypr0fane/32/8337_2.png) [@bennypr0fane](https://discourse.codecombat.com/u/bennypr0fane)\
**Post date:** [June 17, 2018, 7:42pm UTC](https://discourse.codecombat.com/t/level-mad-maxer-sells-out-problem-solved/3660/6 "2018-06-17T19:42:17Z")

</div>

I’m also stumped here. Hero collects some coins, but some disappear before he gets to them, level goals remain incomplete. He doesn’t go to the closest one first, but just some random one.

```python
while True:
    closestGold = None
    minGoldDist = 9001
    coinIndex = 0
    coins = hero.findItems()
    # Finde die nächste Goldmünze.
    # Beachte das Goldmünzen einen Wert (value) von 3 haben
    while coinIndex < len(coins):
        coin = coins[coinIndex]
        if coin:
            distance = hero.distanceTo(coin)
            if distance < minGoldDist and coin.value == 3:
                closestGold = coin
                minGoldDistance = distance
                pass
            coinIndex += 1
            pass
    if closestGold:
        #Begib dich zur nächsten Goldmünzen und bekomme sie!
        hero.moveXY(closestGold.pos.x, closestGold.pos.y)
        pass

```

I wonder why the initial value settings:

```python
    closestGold = None
    minGoldDist = 9001
    coinIndex = 0

```

are under the while True loop - because that means the values get reset at each iteration - and each gold coin that happens to turn out first in the array gets set as variable `closestGold`. But that’s the pre-written code, so it must be correct…?

---

<div class="post-metadata">

**Author:** ![MunkeyShynes](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/munkeyshynes/32/9277_2.png) [@MunkeyShynes](https://discourse.codecombat.com/u/MunkeyShynes)\
**Post date:** [June 18, 2018, 1:40am UTC](https://discourse.codecombat.com/t/level-mad-maxer-sells-out-problem-solved/3660/7 "2018-06-18T01:40:24Z")

</div>

This line, `minGoldDistance = distance` is wrong.

Fix that one line and it works fine.

The pre-written code has to be in the `while True` loop because the code is iterating and incrementing through an array. If only one coin were to appear then yes, it would be set as closestGold, but you’ll notice that three appear at a time.

---

<div class="post-metadata">

**Author:** ![bennypr0fane](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/bennypr0fane/32/8337_2.png) [@bennypr0fane](https://discourse.codecombat.com/u/bennypr0fane)\
**Post date:** [June 18, 2018, 11:44am UTC](https://discourse.codecombat.com/t/level-mad-maxer-sells-out-problem-solved/3660/8 "2018-06-18T11:44:10Z")

</div>

Got it, thanks! 20 characters

---

<div class="post-metadata">

**Author:** ![Alfonso\_Carrejo](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/alfonso_carrejo/32/10513_2.png) [@Alfonso\_Carrejo](https://discourse.codecombat.com/u/Alfonso_Carrejo)\
**Post date:** [July 9, 2019, 4:30am UTC](https://discourse.codecombat.com/t/level-mad-maxer-sells-out-problem-solved/3660/9 "2019-07-09T04:30:41Z")

</div>

im getting infinite loop ,could i get some help please

---

<div class="post-metadata">

**Author:** ![brooksy125](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/brooksy125/32/9966_2.png) [@brooksy125](https://discourse.codecombat.com/u/brooksy125)\
**Post date:** [July 10, 2019, 1:11am UTC](https://discourse.codecombat.com/t/level-mad-maxer-sells-out-problem-solved/3660/10 "2019-07-10T01:11:51Z")

</div>

Please post your code with proper formatting so we can analyze it and help you identify the cause of the infinite loop.

---

<div class="post-metadata">

**Author:** ![Alfonso\_Carrejo](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/alfonso_carrejo/32/10513_2.png) [@Alfonso\_Carrejo](https://discourse.codecombat.com/u/Alfonso_Carrejo)\
**Post date:** [July 12, 2019, 9:15pm UTC](https://discourse.codecombat.com/t/level-mad-maxer-sells-out-problem-solved/3660/11 "2019-07-12T21:15:23Z")

</div>

![Capture](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/7/7b27937696487aefef305bb6e1ca2b82ce89b41c.png)

---

<div class="post-metadata">

**Author:** ![Shurutsue](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/shurutsue/32/10314_2.png) [@Shurutsue](https://discourse.codecombat.com/u/Shurutsue)\
**Post date:** [July 12, 2019, 9:18pm UTC](https://discourse.codecombat.com/t/level-mad-maxer-sells-out-problem-solved/3660/12 "2019-07-12T21:18:55Z")

</div>

You should increase the coinIndex outside of that if-block, otherwise it’ll never “increase” the index if it doesn’t fulfill the conditions and thus never reach coins.length, resulting in an infinite loop.  
Also, you probably want to check if the value is 3 in that if block, not if the coin is 3. 😅

---

<div class="post-metadata">

**Author:** ![Alfonso\_Carrejo](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/alfonso_carrejo/32/10513_2.png) [@Alfonso\_Carrejo](https://discourse.codecombat.com/u/Alfonso_Carrejo)\
**Post date:** [July 16, 2019, 11:57pm UTC](https://discourse.codecombat.com/t/level-mad-maxer-sells-out-problem-solved/3660/13 "2019-07-16T23:57:06Z")

</div>

Is the main topic here considered global varibles ? so i can study the structure some more,this is my tipping point honestly.i cannot get any further past this. yet that is 🙂

---

<div class="post-metadata">

**Author:** ![brooksy125](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/brooksy125/32/9966_2.png) [@brooksy125](https://discourse.codecombat.com/u/brooksy125)\
**Post date:** [July 17, 2019, 3:11pm UTC](https://discourse.codecombat.com/t/level-mad-maxer-sells-out-problem-solved/3660/14 "2019-07-17T15:11:08Z")

</div>

With this level and your current code, the challenge is the while loop more than anything. That is creating your infinite loop like @Shurutsue stated. I’ve found the trick with javascript is finding where the curly braces are starting and ending to make sure the group of code is in the right set of braces. To ensure the index is increasing, it needs to be in the correct set of braces. Currently, your isn’t quite correct. Below is the overall picture you want to see with while loops and index for most basic situations. The increasing index `coinIndex++` should be the last line of code before you close the while loop.

```python
while (coinIndex < coins.length){
    //Other code goes here {
    }
    coinIndex++
}

```

To cover some basics on variables (both Global and Local), along with their attributes, check out the link below. The big idea, a variable is Global when available to all the code, while the Local variables are specific to a group of code like a function or loop.

[Variables - Global and Local](https://python-textbok.readthedocs.io/en/1.0/Variables_and_Scope.html)
