Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
While loop Get names up to five times count=0 while count< 5: name=input(‘Guess my name.\n’) if name==‘kim’: break count= count+1 For loop Convenient when the number of iterations is known count=0 while count< 5: for count in range(5): name=input(‘Guess my name.\n’) name=input(‘Guess my name.\n’) if name==‘kim’: if name==‘kim’: break break count= count+1 FOR loop “for i in range(max):” “i” is a dummy variable keeping counts in the range “i” starts at 0, increments by 1, and ends at (max-1) max=4: i=0, 1, 2, 3 Block of statements (instructions) to be repeats is indented “:” at the end of “for” General FOR loop “for i in range(min, max, interval):” “i” “i” = min min+interval min+interval+inteval …. “inteval” is omitted if interval==1 More about ‘for’ loop What will be printed x=0 for i in range(3): x=x+1 print(x) for x in range(0,3): x=x+1 print(x) What will be printed for i in range(-2,3): print(i) for count in range(5,-5,-2): print(count) “for” loop for p in range(min, max, interval): block p=min p >= max false Do block p = p + interval Out of “for” true Importing modules • Modules are pre-programmed package including useful tools/resources • math • bioPython • pyGame turtle module Simple graphics programming How to use it ? Import ‘turtle’ module by including import turtle Need to create a turtle, and NAME it to manipulate Say, you named a turtle ‘xxx.’ xxx = turtle.Turtle() or turtle.Pen() Opens up a turtle window Can have more than one turtles at a time – tell them apart by names turtle module >>> import turtle >>> myT = turtle.Turtle() What is “.” between turtle and Turtle() ? turtle => class (generic object referring to turtle module) Turtle() => method (action, function, constructor) Rough interpretation: get ‘turtle’ type (class) and do ‘Turtle()’ >>> myT.color(“red”) Get ‘myT’ turtle and color it red Attribute Position, heading (direction), color, tail position Turtle in Python Actions With myT myT.forward(100), myT.backward(100) myT.right(90), myT.left(45) myT.goto(-200,90), myT.circle(50), myT.color(“red”) myT.up(), myT.down() myT.write(“Hello!”) References http://www.eg.bucknell.edu/~hyde/Python3/TurtleDirections.html Official Python turtle page https://docs.python.org/release/3.1.3/library/turtle.html Figure 1.9 Which methods to use ? Suppose ‘myT’ turtle is created Draw a circle --- myT.circle(100) Draw a thicker circle – myT.width(5) Find out the location (x,y) of myT – myT.position() Move myT to (100,-50) – myT.goto(100,-50) Stop drawing – myT.up() Find out the screen sizes screen=myT.getscreen() screen.window_width() and screen.window_height() HW 2: due 9/23 (Fri) Draw olympics rings penup() Goto a location pendown() Draw a circle Take the screen shot which includes your python codes and the drawing to [email protected]