Most of the pseudocode should be familiar except for the two middle lines where Zumi makes a decision based on the orientation. If statements, or conditionals, are used to make decisions within code. You actually use conditionals every day!
The alarm is the condition that needs to be true to wake up. Else, if the condition is false, the program will execute something different. In Python, if statements follow a format similar to this one:
if alarm goes off:
I will wake up
else:
I will stay asleep
Before running the code below, see the values of variables x and y. Can you predict what the output will be?
In [ ]:
x = 10
y = 30
if x > y:
print("x is greater than y")
elif x == y:
print("x is equal to y")
else:
print("x is less than y")
Notice the double equal signs == for checking if two values are equal. This is the comparator you will be using to compare the value of the orientation to the values that you choose. Using your knowledge of for loops, go_straight(), and get_orientation(), fill in the code below to have Zumi drive forward for 200 steps unless Zumi’s orientation is upside down. Don’t forget to put a stop() at the end of the for loop!
# TODO: Reset angles to zero
for x in range("CHANGE ME!"):
zumi.update_angles()
orientation = zumi.get_orientation()
# TODO Check if orientation is equal to 5 and go straight if true
# TODO Else, stop.
# TODO Add one more stop here