COSC 1306 COMPUTER SCIENCE AND PROGRAMMING JehanFranois Pris
- Slides: 38
COSC 1306 COMPUTER SCIENCE AND PROGRAMMING Jehan-François Pâris jfparis@uh. edu Fall 2016
THE ONLINE BOOK CHAPTER VIII MORE ABOUT ITERATIONS
Motivation n We will learn to control better our iterations ¨ while
How much did I spend today? expenses = [ 17. 95, 15. 95, 3. 99, 5. 99] total = 0 for item in expenses : total += item print("Total: %. 2 f" % total)
Letting user enter the expenses total = 0 for i in range(0, 4) : item = float(input("Enter amount: ")) total += item print("Total: %. 2 f" % total) Only works if we know how ahead of time how many purchases we made that day
A more general solution n = int(input("How many purchases: ")) total = 0 for i in range(0, n) : item = float(input("Enter amount: ")) total += item print("Total: %. 2 f" % total) User must still count manually the number of purchases
The while loop n while CONDITION : STATEMENTS repeat STATEMENTS as long as CONDITION is True ¨ Will n Note ¨ The colon ¨ The indentation
The while loop n while condition : something True condition False n while stain : keep_washing something Go back!
while and for n While loops: ¨ Gives more control to the user ¨ Will result in an infinite loop if while contion remains unchanged n For loops: ¨ Simpler and safer ¨ Leaves the user with much less control
How much did I spend today? n Will let the user indicate end of inputs by entering a zero value ¨ "Enter an item or zero when done"
The program total = 0 not. Done = True while not. Done: item = float(input("Enter amount: ")) if amount != 0 : total += item else : not. Done = False print("Total: %. 2 f" % total)
Explanations Initialize accumulator and total = 0 not. Done = True while condition while not. Done: item = float(input("Enter amount: ")) if amount != 0 : Test total += item Keep going else : not. Done = False No more iterations print("Total: %. 2 f" % total)
Back to our roots def mysqrt(number) : root = number/2 for i in range(10) : inverse = number/root = (root + inverse)/2 return root n We always do 10 iterations n We should stop when we have the right relative precision ¨|root – inverse|/root < eps
A better solution def mysqrt(number) : eps = 1 E-10 root = number/2 done = False while not done : inverse = number/root = (root + inverse)/2 if abs(root - inverse)/root < eps : done = True return root # outside the while!
Why the relative error? n Because it is the one that matters ¨ One cm matters when you drill a hole ¨ One meter matters much less when you compute the distance between two big cities ¨… ¨ One milligram matters for some drugs ¨ One kilogram matters much less when you order a ton of dirt.
A missing construct n n Python has no equivalent to the do-while construct of C, C++ and Java ¨ do { STATEMENTS } while(CONDITION); Same is true for Ruby but Lua has ¨ repeat STATEMENTS until CONDITION
Back to the turtles n Random walk STOP
The rules n n Turtle starts at center of canvas Randomly decide to turn 90 o right or left Walks 50 steps Repeats above steps until it hits a border of the canvas The main idea is that turtle motions are unpredictable
A better set of rules n n Turtle starts at center of canvas Randomly decide to turn 90 o right or left Walks 50 steps Repeats above steps while it remains inside the canvas
Pseudocode n While the turtle is still in the window : ¨ generate a random integer between 0 and 1 ¨ if the number == 0 (heads): n turn left ¨ else: n turn right ¨ move the turtle forward 50
Checking if the turtle is inside (I) def is. In. Screen(wn, t): left. Bound = -(wn. window_width()/2) right. Bound = wn. window_width()/2 top. Bound = wn. window_height()/ 2 bottom. Bound = -(wn. window_height()/2) turtle. X = t. xcor() turtle. Y = t. ycor()
Checking if the turtle is inside (II) still. In = True if turtle. X > right. Bound or turtle. X < left. Bound: still. In = False if turtle. Y > top. Bound or turtle. Y < bottom. Bound: still. In = False return still. In
Another way to write it (II) if turtle. X > right. Bound or turtle. X < left. Bound or top. Bound or turtle. Y < bottom. Bound : return False else : return True
The program (I) import random import turtle def is. In. Screen(w, t): … t = turtle. Turtle() wn = turtle. Screen() t. shape('turtle')
The program (II) while is. In. Screen(wn, t) : coin = random. randrange(0, 2) # !!! if coin == 0 : t. left(90) else : t. right(90) t. forward(50) wn. exitonclick()
The old art of printing tables n Can use tabs ("t") to align table columns ¨ Works well with fixed-fixed size fonts ¨ Causes surprises with other fonts n Technique was widely used when computers printed listings ¨ Were consulted off-line
A half-spirited example # Table of squares of 2 print("n", 't', "2**n") #table headings print("---", 't', "-----") for x in range(13): # create entries print(x, 't', 2**x)
The outcome n n 2**n ------0 1 1 2 2 4 3 8 4 16 5 32 6 64 …
Two-dimensional iterations n Images are represented on computers as twodimensional arrays of pixels
Notations (I) n each pixel is denoted by its (x, y) coordinates 0 1 2 0 1 r n c m
Notations (II) n Pixel 0, 0 is at top left corner 0 1 2 0 1 r n c m
Notations (II) n Pixel m, n is at bottom right corner 0 1 2 0 1 r n c m
Notations (III) n Pixel c , r is blue 0 1 2 0 1 r n c m
Pixel colors n n n The color of a pixel is defined by the intensity of its three RGB components on a scale of 0 to 255 ¨ Red: 0 to 255 ¨ Green: 0 to 255 ¨ Blue: 0 to 255 0 means no luminance 255 means maximum luminance 255 can be represented with 8 bits
Constructing colors Color Red Green Blue White Black Yellow Magenta Red 255 0 0 255 255 Green 0 255 0 Blue 0 0 255
Pixel primitives (I) Pixel(r, g, b) Pixel(20, 100, 50) get. Red() r = p. get. Red() get. Green() r = p. get. Green() Create a new pixel with 20 red, 100 green, and 50 blue. Return the red component intensity. Return the green component
Pixel primitives (II) Set the red component intensity to 100. set. Red() p. set. Red(100) set. Green() Set the green p. set. Green(45) component intensity to 45. set. Blue() Set the blue p. set. Blue(156) component intensity to 156.
Learning more n The rest of the chapter teaches you how to manipulate images but you have to ¨ Download the module c. Image. py from Git. Hub ¨ Leave it in the same folder as your programs
- Cosc 1306
- Cosc 1306
- Cosc 1306
- Ssd 1306
- Music 1306
- Uil computer science programming problems
- Python programming an introduction to computer science
- Complete my favourite subject is
- Cosc 4p61
- Cosc 4p42
- Cosc 3p91
- Cosc 4368
- Cosc 4p41
- Cosc 4p41
- Cosc
- Cosc 3340
- Cosc 320
- Cosc parameters
- Cosc 2p12
- Cosc 4p41
- Cosc 3340
- 8088 pinout
- Cosc 121
- Cosc 1p02
- Cosc 2p12
- Cosc 3p92
- 1 bit alu
- Cosc 121
- Cosc 121
- Lc3 trap
- Cosc 121
- Cosc -cos d
- Cosc 3p94
- Cosc101
- Is etm recognizable
- Cosc 3340
- Divisun 2000 ie pris
- Paedfusor vs kataria
- Sio psykolog