Computer Brains and Python Functions: Intro To Robots
Computer Brains and Python Functions: Intro To Robots
Intro to Robots
Computers are useless tools.
But given a program to run they
become highly specialized and
useful tools.
Intro to Robots
Computers, Robots and Python
Intro to Robots
How do Robots Think?
...
Intro to Robots
Possible Work Algorithms:
• Distance:
M = width of robot
L = outside length of surface area
W = outside width of surface area
D = distance to travel in a straight line
Intro to Robots
Possible Turning Algorithms:
• Timing:
M = time to travel width of robot
L = time to travel outside length of surface area
W = time to travel outside width of surface area
D = time to travel in a straight line
Intro to Robots
Possible Turning Algorithms:
• Cleanliness:
# This algorithm requires a sensor on the left side of the robot to determine
# if the work surface to the left of the robot has been processed
Intro to Robots
Python Program Structure:
...
main()
Intro to Robots
Python Program Structure:
every robot program starts
with this.
from myro import *
# import other modules
initialize('com4')
def main():
# do something
...
main()
Intro to Robots
Function Calls:
• Recall
>>> type(“32”)
<type ‘string’>
• type() is a function
– Its name is type
– You can pass an argument that can be any expression
– Entering the name, followed by an expression in
parentheses is called “calling the function” or a
“function call”.
– Function calls “return” a value; in the case of type() the
value is a “type” object that prints out as:
<type ‘type_name’>
Intro to Robots
Another Type Example:
Intro to Robots
The id() function:
Intro to Robots
Converting from one Type to Another:
is an integer and
str(32)
is a string
Intro to Robots
Converting from one Type to Another (cont):
Intro to Robots
Converting from one Type to Another (cont):
Intro to Robots
Converting from one Type to Another (cont):
integer division
Intro to Robots
Modules:
Intro to Robots
Math Functions:
dot notation
module name.function name
Intro to Robots
Math Module contents:
Intro to Robots
Other useful Functions:
Intro to Robots
Versions and Uses of range([start],stop,[step]):
http://docs.python.org/library/functions.html
Intro to Robots
Slicing Lists and Other Things:
http://www.diveintopython.org/native_data_types/lists.html
Intro to Robots
More List Stuff
def rev(L):
stop = -1*len(L)-1
return L[-1:stop:-1]
Intro to Robots
Defining Functions:
• General Syntax:
def function_name (list of parameters):
statements # indented
Intro to Robots
More Functions:
def newline():
print
Intro to Robots
Functions of functions:
• Alternative definition:
This is called a for-loop. A variable, i,
def threeLines(): takes on every value in range(3), which
for i in range(3): we know to be the list [1,2,3].
nl() We don’t actually use the value of i but
we could, inside the for-loop block.
Intro to Robots
For Loop:
• General Format
for <variable> in <sequence>:
do something
do something
...
• For loops are used when you want to do the same thing
over an over again and you know how many times in
advance.
Intro to Robots
Exercise:
Intro to Robots
Intro to Robots
Exercise:
def nineLines():
threeLines()
threeLines()
threeLines()
def twentySevenLines():
nineLines()
nineLines()
nineLines()
Intro to Robots
Intro to Robots
Name this function:
Intro to Robots
Execution flow: # happy_birthday_2.py
from myro import *
def getName():
#happy_birthday_1.py return ask("Whose birthday is it? ")
from myro import *
def happyBirthday(name):
name = ask("Whose birthday is it? ") print "Happy Birthday to you,"
print "Happy Birthday to you,"
print "Happy Birthday to you," print "Happy Birthday to you,"
print "Happy Birthday to you," print "Happy Birthday dear", name+’,’
print "Happy Birthday to you," print "Happy Birthday to you."
print "Happy Birthday dear", name+’,’
print "Happy Birthday to you." def getAgeOf(name):
return ask("How old are you, "+name)
age = ask("How old are you, "+name)
# same program with function calls
print name, "is", age, "years old." def main():
bd_name = getName()
happyBirthday(bd_name)
NOTE: These programs have the age = getAgeOf(bd_name)
same execution flow but one is easier print name, "is", age, "years old.“
to read than the other.
main()
Intro to Robots
Parameters and Arguments:
def happyBirthday(name):
print "Happy Birthday to you,"
print "Happy Birthday to you,"
print "Happy Birthday to you,"
print "Happy Birthday dear", name+’,’
print "Happy Birthday to you."
...
happyBirthday(bd_name)
argument, part of function call
Intro to Robots
Parameters and Arguments (cont):
Intro to Robots
Parameters and Arguments (cont):
• That space on the top of the cake when the name will go
is just like the parameter name in the definition of a
function. It is a place where we can “plug in” an
argument value when we want to use the function.
• For that reason, function parameters are often called
place holders.
• The name of the argument can be anything. You can
write anyone’s name on a blank birthday cake.
Intro to Robots
Exercise:
Intro to Robots
Parameters:
def y(name):
...
Intro to Robots
Local Variables:
Intro to Robots
Variable Scope
Intro to Robots
Using Variables Globally:
x=1
def a():
global x
y=x+2
return y
print a()
Intro to Robots
Misuse of Variables:
x=1
x=1
local variable, x
def a():
x=4 def a():
return x y=x+2
return y
41 UnboundLocalError
Intro to Robots
Two variables called x:
Intro to Robots
The Stack:
Intro to Robots
Intro to Robots