Karel Can't Turn Right - Python
Summary
TLDRThis video tutorial introduces the concept of functions in programming, specifically for a character named Carol in a 'make a tower' exercise. It explains that Carol can't turn right with her basic commands, but the issue can be resolved by using functions to teach her new commands. The video demonstrates how to define a function in Python to make Carol turn right by turning left three times, emphasizing the importance of defining functions before calling them. It also shows how to implement the new 'turn right' function within the program to control Carol's movements effectively.
Takeaways
- 🤖 Carol, the robot, cannot turn right using the original four commands.
- 🔄 As a workaround, Carol can turn left three times to effectively face the same direction as if it had turned right.
- 📚 Introduction of functions as a way to teach Carol new commands beyond the initial set.
- 🔑 The term 'function' may also be known as 'procedure', 'task', or 'process', but in this context, it is primarily referred to as a function.
- 👨🏫 A function in Python is defined using 'def' (short for define), followed by the function name, parentheses, and a colon.
- 🔧 Indentation in Python is used to delineate the body of a function from the rest of the code.
- 🛑 Functions must be defined before they are called in a Python program; the program reads from top to bottom.
- 🔄 Demonstration of teaching Carol to turn right by defining a function that contains the sequence of 'turn left' commands.
- 📝 The function body includes the specific commands that make up the new 'turn right' command for Carol.
- 🔑 Calling the function 'turn right' in the code will execute the defined sequence of commands, enabling Carol to turn right.
- 🎓 Encouragement for the viewer to practice and experiment with functions in the programming editor.
Q & A
Why can't Carol turn right in the script?
-Carol can't turn right because she only knows the original four commands and 'turn right' is not one of them.
What is the alternative method to make Carol face East if she can't turn right?
-The alternative method is to make Carol turn left three times, which results in her facing East.
What is a function in the context of the script?
-A function is a way to teach Carol new commands or words, breaking away from the original four commands she knew.
What is the purpose of teaching Carol new commands with functions?
-The purpose is to expand Carol's capabilities beyond the original commands, allowing her to perform more complex tasks.
What does 'def' stand for in the script?
-'def' stands for 'define', which is used to start the definition of a new function in Python.
How does Python use indentation to delineate code?
-Python uses indentation to indicate the start and end of a block of code, such as the body of a function.
Why is it necessary to define a function before calling it in Python?
-In Python, functions must be defined before they are called because Python reads from the top down, and it needs to know what the function does before it is executed.
What is the error that occurs if you try to call a function that hasn't been defined yet in Python?
-An error will occur stating that the function is not defined, with a reference to the line number where the call was made.
How does the script demonstrate teaching Carol a new command?
-The script demonstrates it by defining a function called 'turn right' and then calling that function to make Carol execute the new command.
What is the significance of the 'make a tower' exercise in the script?
-The 'make a tower' exercise is used as a practical example to illustrate how to teach Carol new commands using functions.
How does the script suggest to fix the issue of Carol not being able to turn right?
-The script suggests creating a function named 'turn right' that contains the sequence of commands to turn left three times, effectively turning right.
Outlines
🤖 Teaching Carol New Commands with Functions
The video introduces the concept of functions as a method to teach Carol, a robot, new commands beyond her original four. The issue of Carol's inability to turn right is highlighted, and a workaround of turning left three times is discussed. The main focus is on defining a function in Python to enable Carol to turn right. The process involves using the 'def' keyword, giving the function a name, and using indentation to define the function's body, which in this case includes three 'turn left' commands to achieve a right turn. The video emphasizes the importance of defining functions before using them in Python due to its top-down reading order, and demonstrates how to integrate the new 'turn right' function into Carol's existing code.
📝 Implementing the TURN RIGHT Function in Python
This paragraph continues the discussion on functions by demonstrating how to implement the newly defined 'turn right' function in Carol's code. It explains the importance of proper indentation in Python to distinguish between the function body and the rest of the code. The video shows a practical example of modifying Carol's code to include the 'turn right' function, ensuring it is placed at the top as per Python's requirement. The demonstration includes running the code to check if the function works correctly and adjusting the code to call the 'turn right' function, resulting in Carol successfully turning right. The viewer is then encouraged to practice using functions in the Python editor.
Mindmap
Keywords
💡Carol
💡Commands
💡Turn Right
💡Functions
💡Define
💡Indentation
💡Order of Functions
💡Make a Tower Exercise
💡Turn Left
💡Call a Function
Highlights
Carol, the robot, cannot turn right using its original commands.
A workaround for Carol's inability to turn right is to turn left three times.
Functions are introduced as a method to teach Carol new commands.
Functions can also be called procedures, tasks, or processes.
The 'def' keyword is used to define a new function in Python.
Indentation is crucial in Python to delineate the function body.
Functions must be defined before they can be called in Python.
The order of function definitions matters in Python due to its top-down reading.
A practical example of teaching Carol to turn right using a function is given.
The 'turn right' command is created by defining a function with three 'turn left' commands.
Executing a function in Python requires calling it after its definition.
An error occurs if a function is called before it is defined in Python.
The video demonstrates using the 'turn right' function in a Karel programming exercise.
The 'put ball' and 'move' commands are used in the Karel exercise.
The video shows a step-by-step process of teaching Carol a new command through functions.
The importance of correctly indenting commands within a function is emphasized.
The video concludes with Carol successfully turning right using the newly defined function.
The video encourages viewers to experiment with functions in the Karel editor.
Transcripts
hi in this video we're going to talk
about something that you may have
already noticed and that is Carol can't
turn right so let's think back to the
make a tower exercise so you may have
gotten to a point in the program where
you got Carol to the top of the tower
and you wanted Carol to turn right to
face East you may have even typed out
turn right and you probably found out
that it did not work
Carol cannot turn right Carol only knows
those original four commands so instead
what can we do you've probably
discovered that instead of turning right
we can turn left three times and achieve
the same results Carol will go from
north through west south and then
finally end up facing east so Carol will
pretty much turn right but that's not
really the best way to do it would it be
nice if we could just say turn right
that is really what we want Cara to do
we don't want Carol to have to turn left
three times so how can we do that
introducing functions functions are a
way we can teach Carol new words so what
is a function it's a way to teach Carol
new words and break away from the
original four commands that we had to
start with and we can teach Carol a new
command
you might hear functions called
different things such as procedures
tasks processes but in this course we're
mainly going to call them functions we
want to teach Carol how to turn right so
how do we do that
this is how we teach Carol to turn right
we write def short for define and then
we give the function a name open and
close parentheses in a colon then for
every command that we want to be a part
of the function we write it indented by
one tab or one level in Python we use
indentation to help delineate our code
so the function body would contain these
three commands to turn left once we stop
indenting the commands are no longer a
part of the function so in this case the
move command would not be part of our
function
one thing that is different about Python
compared to other languages is that the
order that we put our function in
matters in Python we must define a
function before we can call it when we
go to execute our programs Python reads
from the top down and if it gets to the
command to turn right but hasn't seen
the definition to turn right yet it'll
produce an error so let's take a look at
how we use functions and how we can
teach Karel new commands in the editor
okay so let's take a look at how we can
use this make a tower with a turn write
command so if you think back to what we
did we probably started off with a move
command and then we would have said turn
left let me go back and check this so
far you can see okay we've done that now
we need to put a ball down we're gonna
say put ball and then move okay let's go
check that again okay so we're off to a
good start let's continue from here so
now we want to we want to put another
ball then we're going to move put ball
and move let's run that right there
we're going to reset and run okay so now
we're at the top of our pyramid we're
close to having the the result world
here but we need to do is if we look at
our exercise we need to have Karel
facing east and right now Cairo is
facing north so what we want to try is
something like turn right and if we do
that and we go to run we're gonna see
uh-oh
turn right is not defined on line 9 so
if you remember we probably to solve
this said let's go and turn left three
times
and that would solve it for us so let's
go ahead and reset and then we'll try
running that so there it goes Carol up
the tower turns left and nice job we got
it okay so now what we want to do though
is we want to make that turn right
command so now remember in Python we got
to put that command up top here and so
we're going to make a function and we're
going to teach Carol a new word and
we're going to teach Cairo that TURN
RIGHT command so we start off with def
for define and then we're gonna say turn
right so we're gonna teach Carol how to
turn right okay now our format in Python
we put a colon after our definition and
now notice how we start off indenting a
line there so now we want to take these
three commands and we're going to take
them and put them up in Arkham in there
so no matter make sure that we are
indenting all the same
remember our code that's indented as
part of our function and our code that's
not indented is not part of our function
so all these commands will not be part
of our function all of these are so if I
go ahead and reset you'll notice the
program just really skips over that turn
right and it comes up and we haven't
turned right yet because we've only told
Carol how to do that command we haven't
actually asked Carol to do it so we're
going to go down here and now we're
going to ask Carol to turn right okay so
we're going to go back and reset that
now that we're calling our function it
should execute these commands and you'll
notice as it goes and executes the
commands this line that's showing where
we're executing is going to jump up into
that function and come down and run the
lines and there we go we've now talked
Carol how to turn right so now it's your
turn to play around in the editor
5.0 / 5 (0 votes)