PYTHON
  • PYTHON v3
  • Python IDE Setup
  • Python Programming
    • Python for ABS
      • Resources
      • Ch1 - Getting Started
      • Ch2 - Types, Variables and Simple I/O
  • Python For Network Engineers-Kirk Beyers
    • Resources
    • Python Fundamentals
  • Python Inststitute (PCAP)
    • Resources
    • Course Introduction
    • Python Essentials (Part 1)
      • Module 1
        • Fundamentals
        • What is Python
        • Starting your work with Python
      • Module 2
        • The Print Function
          • Section Summary
        • Literals
          • Section Summary
        • Operations- Data Manipulation Tools
          • Section Summary
        • Variables
          • Leaving Comments in Code
          • The input () Function
  • 100 Days Of Code
    • Resources
    • What You Have To Do !!
    • 100 DAY's
      • DAY 1: Working with Variables
      • Day 2: Data Types & String Manipulation
      • Day 3: Control Flow and Operators
      • Day 4: Randomisation and Lists
      • Day 5: For Loops, Range & Code Blocks
      • Day 6: Python Functions and Karel
      • Day 7: Hangman
      • SUMMARY
  • {Pirple}
    • Resources
Powered by GitBook
On this page
  • Resource:
  • Functions
  • While Loops
  • Example:1
  • When to use For Loops and When to use While Loops:
  • Example 2
  • Example 3:
  • Project of the Day: Escaping the Maze
  • Step1: Turn Right
  • Step2:

Was this helpful?

  1. 100 Days Of Code
  2. 100 DAY's

Day 6: Python Functions and Karel

Functions, Code Blocks and While Loops

PreviousDay 5: For Loops, Range & Code BlocksNextDay 7: Hangman

Last updated 4 years ago

Was this helpful?

Resource:

Functions

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.

def turn_right():
    turn_left()
    turn_left()
    turn_left()
    
def steps():
    move()
    turn_left()
    move()
    turn_right()
    move()
    turn_right()
    move()
    turn_left()

for jump in range(6):
    steps()

While Loops

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

def turn_right():
    turn_left()
    turn_left()
    turn_left()
    
def steps():
    move()
    turn_left()
    move()
    turn_right()
    move()
    turn_right()
    move()
    turn_left()

number_of_hurdles = 6
while number_of_hurdles > 0:
    steps()
    number_of_hurdles -= 1
    print(number_of_hurdles)

Example:1

When to use For Loops and When to use While Loops:

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

Example 2

REMEMBER IF STATEMENTS CAN GO INSIDE LOOPS -- IDIOT!!!!!

def turn_right():
    turn_left()
    turn_left()
    turn_left()
    
def steps():
    #move()
    turn_left()
    move()
    turn_right()
    move()
    turn_right()
    move()
    turn_left()


#while at_goal() != True:
#    steps()
#print(at_goal)

while not at_goal():
    if wall_in_front():
        steps()
    else:
        move()

Example 3:

def turn_right():
    turn_left()
    turn_left()
    turn_left()
    
def steps():
    #move()
    turn_left()
    while wall_on_right():
        move()
        
    turn_right()
    move()
    turn_right()
    while front_is_clear():
        move()
    turn_left()
 
#while at_goal() != True:
#    steps()
#print(at_goal)

while not at_goal():
    if wall_in_front():
        steps()
    else:
        move()

Project of the Day: Escaping the Maze

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

Step1: Turn Right

This step is to define a function - using the def <name>() method, of turning right

Step2:

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

def turn_right():
    turn_left()
    turn_left()
    turn_left()
    

while not at_goal():
    if right_is_clear():
        turn_right()
        move()
    
    elif front_is_clear():
            move()
            
    else:
        turn_left()
Reeborg's World
Stuck in an infinite loop !!