Presentation is loading. Please wait.

Presentation is loading. Please wait.

For vs. While loop count=0 while count< 5: for count in range(5):

Similar presentations


Presentation on theme: "For vs. While loop count=0 while count< 5: for count in range(5):"— Presentation transcript:

1 For vs. While loop count=0 while count< 5: for count in range(5):
name=input(‘Guess my name.\n’) if name==‘kim’: break count= count+1 for count in range(5): name=input(‘Guess my name.\n’) if name==‘kim’: break

2 “for” loop p=min true false for p in range(min, max, interval): block
p >= max false Do block p = p + interval Out of “for”

3 turtle module Simple graphics programming How to use it ?
Import ‘turtle’ module by including from turtle import * Need to create a turtle, and NAME it to manipulate Say, you named a turtle ‘xxx.’ xxx = Turtle() or Pen() Opens up a turtle window Can have more than one turtles at a time – tell them apart by names

4 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

5 Turtle in Python Actions References 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 Official Python turtle page

6 Figure 1.9

7 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()

8 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

9 While Function eval() returns the result of an arithmetic operation
For the English description below, write Python function (and a call to the function) Ask to enter a simple arithmetic operation or ‘q’ to stop As long as the input is not ‘q’ print the result of the arithmetic operation Ask the same question again

10 Iteration – drunken turle
Random walk or Brownian motion A turtle From the current position Take an arbitrary direction and distance as the next position Goto the next position Next position becomes the current position next time around

11 Random package import random
random.randint(100) returns a random integer How to get a random number between (0,100)? A random number between (-50, 50) ?

12 Drunken Turtle With For loop
Create a turtle, name it on your own For 200 times, repeat Get the distance to the next random position from (curX, curY) Add the distance to (curX, curY) to compute the new random location Move the turtle to the new location

13 Lab 0919 Write a Python program to simulate a drunken turtle
Modify the drunken turtle program so that the turtle stays in bound wn = turtle.getscreen() wWid = wn.window_width() wHgt = wn.window_height() Make it sure that you halve the window sizes for +- coordinates your Python program


Download ppt "For vs. While loop count=0 while count< 5: for count in range(5):"

Similar presentations


Ads by Google