[SOLVED] Sarven Sum Python

whiteX = {'x':27, 'y':42}
redX = {'x':151 , 'y': 118}
hero.moveXY(27, 42)
def find():
    hazards= hero.findHazards()
    Index = 0
    minV = 9999
    maxV = 0
    while Index < len(hazards):
        hazard = hazards[Index]
        if hazard:
            if hazard.value < minV:
                minV = hazard.value
            elif hazard.value > maxV:
                maxV = hazard.value
        Index += 1
    return minV
    return maxV
find()
if minV and maxV:
    number = minV + maxV
    hero.say(number)

link
So, am I doing this wrong or smth?

Putting this in a function means that you can’t just take the returned value out - to the computer, minV & maxV are undefined variables. Therefore, we encounter the problem of, how do we find the total of maxV & minV without pulling them out of the function and causing an error? We simply return implicitly, which is the total of maxV & minV! Oh and remove the if statement:

I think you’ll be able to figure out the rest in terms of saying the value.

If you want to read further into the return statement (as it can actually get really confusing) you could bookmark this page so that you can get grips with it.

Hope this helps.

Tip: you can return multiple values in one statement by separating them with commas:
return x, y, z :slightly_smiling_face:

1 Like

whiteX = {'x':27, 'y':42}
redX = {'x':151 , 'y': 118}
hero.moveXY(27, 42)
def find():
    hazards= hero.findHazards()
    Index = 0
    minV = 9999
    maxV = 0
    while Index < len(hazards):
        hazard = hazards[Index]
        if hazard:
            if hazard.value < minV:
                minV = hazard.value
            elif hazard.value > maxV:
                maxV = hazard.value
        Index += 1
    number = minV + maxV
    return number
find()
hero.say(number)

I still can’t figure out how to do this level

1 Like

Remember, don’t say a variable outside of a function or the computer will interpret that as an undefined variable. Try using the function itself. And remember to hightail it out of that canyon - I’ve heard witches lurk in there… :hushed:

Have you completed the level?

No, I still don’t get what you mean

Sorry I didnt respond earlier, been busy with school and havent popped into coco for a while

So let’s recap what I mean: any variables declared in a function cannot be called outside of it because its scope (where the variable can be used) does not reach that far. This is why you get this error:


If you try to use minV outside the function then the computer will think that you have used a variable that has not been defined. To solve this problem, say the function. Why? This return statement:

returns number which means that this function finds the sum of the lowest and highest trap values and returns it. So this must mean that if we said the function, we would really be saying the sum of the lowest and highest trap values. This is why you mustn’t say the variable number, but the function. Do you understand now?

Let’s see what we can do for Diamond Dozen.

1 Like

Now I mean what you get, thank you!

1 Like

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.