Day 6: Python Functions and Karel
Functions, Code Blocks and While Loops
Last updated
Was this helpful?
Functions, Code Blocks and While Loops
Last updated
Was this helpful?
As seen above these are some of the built-in functions that python provides.
Functions are defined as a word or name followed by parenthesis such as print( )
etc.
NOW !!! What if we wanted to make our OWN functions??? OK, cool - but HOW??
To make our own function we need to DEFINE it by using the keyword
def <name> ( )
for example def my_function( ): -------> dont forget the colon!!!
The function is like creating a recipe and then storing it for later. When you need it you "call" the function (recipe) by specifying the name and a set of parentheses and this is then activated. Instead of having to write out the whole recipe EVERY time you need it somewhere in your code, you just pull it out when you need it and this saves you loads of time and make your code easier to read.
The while loop, the loop that will continue going while a particular condition is TRUE!
We have seen 2 flavours of the "FOR loop:
One, where we are looping through a LIST of items to do something with each item in that list.
for item in list-of-items:
do something to each item
and the other is using a RANGE function where we create a range between a and b , and then we use every number in that range to do something.
for number in range(a ,b)
print(number)
The WHILE loop can be written as
while something something_is_true:
do something repeatedly unti the condition chnages to FALSE and then it will jump out of the loop
Remember - for loops are good for iterating over something and you need to do something with each thing you are iterating over. So for example we are iterating through a list of fruits and we want to DO SOMETHING WITH EACH of the items in the list we have to use FOR LOOPS
Similarly we have use the RANGE function such as the one below, its easier to do with a FOR LOOP than a WHILE loop
Now you want to be using a WHILE loop when you don't really CARE what number in a sequence you're in, which item you are iterating through in a list or you just simply want to carry out some sort of functionality many, many times until some sort of condition that you set has been achieved or met.
"While Loops" are more dangeorus than "FOR loops" because in FOR Loops you're setting ahead of time how many times something is going to run. Its going to stop once it reaches the end of a list of items in this case and if the For Range loop it will stop once it reaches the upper bound of the range. BUT ....for "WHILE" loops, they will continue running until a particular condition switches either from True to False or False to True. If a condition somehow never switches you could be caught in an INFINITE LOOP
REMEMBER IF STATEMENTS CAN GO INSIDE LOOPS -- IDIOT!!!!!
There are a few things here at play:
At the start of play, the direction the robot is going to face is random
The position of the robot within in the maze will also be random
The secret to complete the maze (see above) is to have the robot move along the right edge of the maze, TURNING RIGHT if it can and going STRAIGHT AHEAD if it cant turn right and turning LEFT as the last resort
By doing this will eventually get you to the destination. This is called a STRATEGY or rather in programming called an ALGORITHM.
So following along the right side of the wall, and if the RIGHT side is CLEAR, to CONTINUE moving right until its no longer clear at which point it should turn LEFT
This step is to define a function - using the def <name>()
method, of turning right
The next thing to do, is we need to tell the robot to do things and to check for things until it reaches its goal. So we use a "while loop" to get the robot to continue working until it reaches the goal. To define that we will say while not at_goal():
(basically keep repeating the instructions inside this "while loop" until at_goal
becomes TRUE )
So what we were told was to follow along the right edge. We should test to see if the right side is clear, and if it is we should turn right and go straight
Lest use an "if statement" to test if right_is_clear():
we are going to turn right and then once facing right, to move forward.
To fix this we need an alternative condition, when the RIGHT is not clear, what should it do? So we will provide an elif
statement, saying, "if the right is NOT clear, maybe we should check if the FRONT is clear? If it is then simply move forwards