Lost Viking Help needed


#1

Can someone please help me with the Lost Viking Code Challenge? I’m not sure how to make the character continuously go south. Here is the code I have written.

while steps <= 35:

# Take the next step:
self.moveXY(steps * X_PACE_LENGTH, sideSteps * Y_PACE_LENGTH)

# Increment steps and sideSteps as appropriate, taking into account the special rules.
steps += 1
if steps% (SWITCH+1) == 0:
    self.say("Reached SWITCH point")
    sideSteps -= 1
else:
    sideSteps += 1

#2

I used a new variable to change the direction of movement

s=1

while steps <= 35:
    
    # Take the next step:
    self.moveXY(steps * X_PACE_LENGTH, sideSteps * Y_PACE_LENGTH)
    
    # Increment steps and sideSteps as appropriate, taking into account the special rules.
    
    
    if steps%SWITCH ==0:
        s=-s

#3

Well, the problem is that the code will only execute once, meaning the next step is south, and the step after is north again. This is the problem I wish to solve, but thank you for your reply anyway!


#4

Use sideSteps+=s and your problem is solved.


#5
 steps++;
    if(steps % (SWITCH + 1) === 0) {
        s=-s;
        this.say("SWITCH");
    }
    if(steps % (SKIP + 1) === 0) {
        if(s % 2 === 0) {
            s = s/2;
            this.say("SKIP OFF");
        }
        else {
            s *= 2;
            this.say("SKIP ON");
        }
    }
    sideSteps += s;

What’s wrong with my code, can’t really catch the problem


#6

You must sideStep TWICE for that step (this is called SKIPping).

    
    if steps%SKIP ==0:
       sideSteps+=s
    sideSteps+=s
    steps += 1
   

In my programm is steps += 1 in another place , I can use if steps%SKIP ==0:


#7

Thank you very much, i’ve figured out i misunderstood the skip mechanics. I had to double-step only one, not untill i reached the next “SKIP” point.


#8

Hi,

I’m having trouble with my code. At the moment my switch doesn’t work. Here is my code

// Take the next step:
    
    hero.moveXY(steps * X_PACE_LENGTH, sideSteps * Y_PACE_LENGTH);
    hero.say("Steps:");
    hero.say(steps);
    hero.say("sideSteps:");
    hero.say(sideSteps);
    // Increment steps and sideSteps as appropriate, taking into account the special rules.
    
    steps+= 1;
    sideSteps+= 1*PosNeg;
    
    
    if(steps % 6 === 0 ){
        PosNeg == PosNeg*-1;
        }
    
    
    
    if(steps % 9 === 0){
        sideSteps+= PosNeg*1;
    }
    
    if(sideSteps > 10){
     sideSteps = 1;   
    }
    
    if(sideSteps < 1){
        sideSteps = 10;
    }
}

If you could help me that would be great.

Thanks!