Skip to content

Instantly share code, notes, and snippets.

@TheMuellenator
Last active January 18, 2024 20:03
Show Gist options
  • Save TheMuellenator/dc4d84419c38a5aa023f85c26bea2dc7 to your computer and use it in GitHub Desktop.
Save TheMuellenator/dc4d84419c38a5aa023f85c26bea2dc7 to your computer and use it in GitHub Desktop.
Python Functions Coding Exercise - Part 1 Solution
tracker = 0
def moveForwards():
global tracker
tracker += 1
print('moved forward by one step.')
def turnRight():
global tracker
tracker -= 1
print('turning right')
def move():
#Solution
moveForwards()
turnRight()
turnRight()
turnRight()
moveForwards()
turnRight()
turnRight()
turnRight()
moveForwards()
turnRight()
moveForwards()
turnRight()
moveForwards()
moveForwards()
return tracker
@tussarraj
Copy link

can you plz explain me how this tracker is moving ?
moveForwards()
turnRight()
turnRight()
turnRight()
moveForwards()
turnRight()
turnRight()
turnRight()
moveForwards()
turnRight()
moveForwards()
turnRight()
moveForwards()
moveForwards()

@amoghtripathi
Copy link

can you plz explain me how this tracker is moving ?
moveForwards()
turnRight()
turnRight()
turnRight()
moveForwards()
turnRight()
turnRight()
turnRight()
moveForwards()
turnRight()
moveForwards()
turnRight()
moveForwards()
moveForwards()

instead of turning it left by 90deg, here we are turning to right by 270deg

@pavinayan
Copy link

Can you please explain whats the need of global tracker variable ? Why do we need to check its count?

@ismail-boop
Copy link

Could you pleas give an explaination for the given solution:

#Solution
moveForwards()
turnRight()
turnRight()
turnRight()
moveForwards()
turnRight()
turnRight()
turnRight()
moveForwards()
turnRight()
moveForwards()
turnRight()
moveForwards()
moveForwards()

@jaz801
Copy link

jaz801 commented Jan 8, 2022

I hope this explanation works.

moveForward() --> moves one FULL box instead of HALF a box.

@TheMuellenator Could the answer have fewer lines of code if you add this function?

def turnLeft():
       turnRight()
       turnRight()
       turnRight()
       print('turning left')

#Solution 
    moveForwards()
    turnLeft()
    moveForwards()
    turnLeft()
    moveForwards()
    turnRight()
    moveForwards()
    turnRight()
    moveForwards()
    moveForwards()

Copy link

ghost commented Feb 7, 2022

def turnLeft():
global tracker
tracker -= 3
print('turning left')

def move():

moveForwards()
turnLeft()
moveForwards()
turnLeft()
moveForwards()
turnRight()
moveForwards()
turnRight()
moveForwards()
moveForwards()


return tracker

@SanurC
Copy link

SanurC commented Jan 1, 2023

Can you please explain global function in here?

@Dade-Isard
Copy link

cant you just make/trick the tracker into making negative right turns rather than the analog right(), right(), right(), until desired direction?

@jaz801
Copy link

jaz801 commented Apr 14, 2023

If you ever get stuck in coding, just ask ChatGPT; they will make the script for you and you can ask anything to explain it in detail.

@Dade-Isard
Copy link

wonder what its got to say about jesus taking the wheel... i know... i'll ask

@vicentesurraco
Copy link

for those asking about the tracker, since "moveforwards" and "turnright" does not mean anything to Python, the tracker exists to serve as a proxy test to see whether you moved forward or moved right the correct number of times.

@PiotrSochaczewski
Copy link

This exersice have logical issue. We define function move() by calling two functions moveForwards() and trunRight()
BUT
we never call function move(). And this is where the logic error occure: if you only define function you pass exersice, but if you define and call it you fail test. But only defining function doesn't make move so this excersice omite important part of calling the function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment