Chapter 1 – Introduction to Python with Zumis
1.3 – Remote Control and Conditionals
Remote Control
Now that we understand driving commands, let’s try creating our own remote control to drive Zumi!
First, import the Zumi library and create an object instance of Zumi before you begin.
from zumi.zumi import Zumi
import time
zumi = Zumi()
User input
To make a remote control, the next thing we need is a way to get our input and translate it to motor commands to send to Zumi.
Python includes a function input() that reads the keyboard input and saves it as a variable. To see how this works, add the following code to your program, type in your name, and hit enter. Any input that you entered will be saved in the variable called my_name.
my_name = input("What is your name? ")
print("Hello,",my_name)
Pseudocode
Now that we have aset up the base code for our program, let’s create the pseudocode for our program; a program that’s written in plain language so that a human can understand. Pseudocode is a good way to figure out how your program will work before translating it to Python.
One small problem: input() can’t read arrow key controls, so your remote control code will be checking for letter inputs “w”, “a”, “s”, and “d”. Provided below is the Pseudocode that we will use to program our remote control.
get input from user
if input is “w”, go forward
if input is “s”, reverse
if input is “a”, turn left
if input is “d”, turn right
If statements
If statements are conditional statements used in a program and are only executed with the provided condition is met. Code to be run of a condition is met is indented inside the if statement associated with it. Let’s take a look at an example:
PSEUDOCODE
If my alarm goes off, then I will wake up. Else, I will stay asleep.
If I am hungry, I will eat something.
If it’s sunny out, I will wear sunscreen.
If my room is clean, I will get my allowance. Else, I will make no money.
If statements are written like this in Python:
if insert condition here:
Insert statements here
else:
Insert statements here
if alarm goes off:
I will wake up
else:
Stay asleep
Note: The condition is what the computer checks to know if it is true or false and uses it to decide whether to run the code in the if statement or not.
- If the condition is TRUE, the computer will execute, or carry out, the statements underneath it.
- If the condition is FALSE, the computer will NOT execute the statements underneath it.
- ELSE has no condition! This is because “else” is the last option when using if-else statements. If the conditions for all the previous if statements in not met, then the else condition is automatically true and its statements are run.
Note: Be careful with indents! Even if the indent is off by one space, you will get an error! Try to stick to either tabs or spaces, and don’t combine the two.
Comparators
Next, let’s translate our pseudocode to Python code. Before inputting drive commands, let’s use print statements for testing, like this one:
direction = input("Please enter a command: ")
if direction == "w":
print("Go forward")
The equal sign used in this code is an example of a comparator. Comparators can be used inside conditions to compare values. If the statement is true, the statements inside that if statement will run. These are comparators:
> -> Greater Than
< -> Less Than
>= -> Greater Than or Equal To
<= -> Less Than or Equal To
== -> Equal To
!= -> Not Equal To
Let’s run the cell below and make some changes to the conditionals to see how these work! Try changing the variable names or the input statements. Make sure to test and run your code.
X= 10
Y = 100
Z = 200
if X != Y:
print("X is not equal to Y")
if Z > Y:
print("Z is greater than Y")
Now that we know how to use conditionals, let’s use if statements, comparators, and print statements to write code for the remaining “s”, “a”, and “d” keys. Based on what we’ve learned. try to complete the missing parts of the code below.
TODO Finish the rest of the missing if statements
while True:
direction = input("Please enter a command: ")
if direction == "w":
print("Go forward")
if direction == s
if direction == a
if direction == d
Did you notice the code is running forever? Because this is a remote control code, you want to be able to continuously tell Zumi what to do. To do this, you need to use a while loop that runs forever. Currently, the only way to stop the code is by clicking the “stop” icon in the toolbar. To safely quit from this loop, it is better to include an if statement that checks for “q” input to break from the loop.
Add the code below to your program above.
if direction =="q":
break
Testing our remote control
Now that we’re done programming our remote control, let’s try using it and take Zumi for a drive! Try using your remote control to drive Zumi, and see if there are any other driving commands you’d like to add to your program. Happy Driving!
Demo Video
Questions