iteration “for ??? in range(min,max,interval):” what if we do not know the number of repetitions...
TRANSCRIPT
![Page 1: Iteration “for ??? in range(min,max,interval):” What if we do not know the number of repetitions ? “while (???):” (p.49) problem = input(“Enter a math](https://reader036.vdocument.in/reader036/viewer/2022081603/56649f305503460f94c4b6f5/html5/thumbnails/1.jpg)
Iteration“for ??? in range(min,max,interval):”
What if we do not know the number of repetitions ?“while (???):”
(p.49)problem = input(“Enter a math problem, or ‘q’ to quit: “)while (problem != “q”): print(“Answer to “ , problem, “ is: “, eval(problem)) problem = input(“Enter a math problem, or ‘q’ to quit: “)
![Page 2: Iteration “for ??? in range(min,max,interval):” What if we do not know the number of repetitions ? “while (???):” (p.49) problem = input(“Enter a math](https://reader036.vdocument.in/reader036/viewer/2022081603/56649f305503460f94c4b6f5/html5/thumbnails/2.jpg)
Chap. 5 Conditional Expressions
Can test equality with == Can also test <, >, >=, <=, != (not equals) In general, 0 is false, 1 is true
So you can have a function return a “true” or “false” value.
Alert!= means “make them equal!”== means “are they equal?”
![Page 3: Iteration “for ??? in range(min,max,interval):” What if we do not know the number of repetitions ? “while (???):” (p.49) problem = input(“Enter a math](https://reader036.vdocument.in/reader036/viewer/2022081603/56649f305503460f94c4b6f5/html5/thumbnails/3.jpg)
Multiple conditionals (Boolean) A and B A or B
True only when both True when either A and B are true A or B is true
B\A False True B\A False True
False False False False False True
True False True True True True
not A
![Page 4: Iteration “for ??? in range(min,max,interval):” What if we do not know the number of repetitions ? “while (???):” (p.49) problem = input(“Enter a math](https://reader036.vdocument.in/reader036/viewer/2022081603/56649f305503460f94c4b6f5/html5/thumbnails/4.jpg)
Boolean Operators
and, or, not 6<10 and 3<7 4!=4 or 5<8 not 6<10
![Page 5: Iteration “for ??? in range(min,max,interval):” What if we do not know the number of repetitions ? “while (???):” (p.49) problem = input(“Enter a math](https://reader036.vdocument.in/reader036/viewer/2022081603/56649f305503460f94c4b6f5/html5/thumbnails/5.jpg)
If..else Conditional
def posSum(list): psum = 0 for v in range(0, len(list)): if list[v] > 0: psum = psum+list[v] return psum
Add all values in a list
Add only positive values
def sum(list): psum = 0 for v in range(0, len(list)): psum = psum + list[v] return psum
![Page 6: Iteration “for ??? in range(min,max,interval):” What if we do not know the number of repetitions ? “while (???):” (p.49) problem = input(“Enter a math](https://reader036.vdocument.in/reader036/viewer/2022081603/56649f305503460f94c4b6f5/html5/thumbnails/6.jpg)
How an if works
if is the command name Next comes an
expression: Some kind of true or false comparison
Then a colon
Then the body of the if—the things that will happen ONLY WHEN the expression is true
if list[v] > 0:
psum = psum+list[v]
![Page 7: Iteration “for ??? in range(min,max,interval):” What if we do not know the number of repetitions ? “while (???):” (p.49) problem = input(“Enter a math](https://reader036.vdocument.in/reader036/viewer/2022081603/56649f305503460f94c4b6f5/html5/thumbnails/7.jpg)
if (expression):
next indented block
else: Statements
false
Pictorial view of how if works
true
![Page 8: Iteration “for ??? in range(min,max,interval):” What if we do not know the number of repetitions ? “while (???):” (p.49) problem = input(“Enter a math](https://reader036.vdocument.in/reader036/viewer/2022081603/56649f305503460f94c4b6f5/html5/thumbnails/8.jpg)
ConditionalDrunken turtle
Random walk or Brownian motionA turtle
From the current positionTake an arbitrary direction and distance
![Page 9: Iteration “for ??? in range(min,max,interval):” What if we do not know the number of repetitions ? “while (???):” (p.49) problem = input(“Enter a math](https://reader036.vdocument.in/reader036/viewer/2022081603/56649f305503460f94c4b6f5/html5/thumbnails/9.jpg)
Random packageimport random
random.random() returns a random fraction
between 0 and 1
How to get a random number between (0,100)?
A random number between (-50, 50) ?
![Page 10: Iteration “for ??? in range(min,max,interval):” What if we do not know the number of repetitions ? “while (???):” (p.49) problem = input(“Enter a math](https://reader036.vdocument.in/reader036/viewer/2022081603/56649f305503460f94c4b6f5/html5/thumbnails/10.jpg)
Drunken Turtle
Present location of turtle: curX, curY
Next random location ?nextX = curX -50 + 100* random.random()nextY = curY -50 + 100* random.random()
![Page 11: Iteration “for ??? in range(min,max,interval):” What if we do not know the number of repetitions ? “while (???):” (p.49) problem = input(“Enter a math](https://reader036.vdocument.in/reader036/viewer/2022081603/56649f305503460f94c4b6f5/html5/thumbnails/11.jpg)
Random Walk
import random
## function randNext(curX, curY)# Given coordinate (curX, curY), generate next
random coordinate#def randNext(curX, curY): nextX = curX -50 + 100*random.random() nextY = curY -50 + 100*random.random() return nextX, nextY
![Page 12: Iteration “for ??? in range(min,max,interval):” What if we do not know the number of repetitions ? “while (???):” (p.49) problem = input(“Enter a math](https://reader036.vdocument.in/reader036/viewer/2022081603/56649f305503460f94c4b6f5/html5/thumbnails/12.jpg)
![Page 13: Iteration “for ??? in range(min,max,interval):” What if we do not know the number of repetitions ? “while (???):” (p.49) problem = input(“Enter a math](https://reader036.vdocument.in/reader036/viewer/2022081603/56649f305503460f94c4b6f5/html5/thumbnails/13.jpg)
![Page 14: Iteration “for ??? in range(min,max,interval):” What if we do not know the number of repetitions ? “while (???):” (p.49) problem = input(“Enter a math](https://reader036.vdocument.in/reader036/viewer/2022081603/56649f305503460f94c4b6f5/html5/thumbnails/14.jpg)
![Page 15: Iteration “for ??? in range(min,max,interval):” What if we do not know the number of repetitions ? “while (???):” (p.49) problem = input(“Enter a math](https://reader036.vdocument.in/reader036/viewer/2022081603/56649f305503460f94c4b6f5/html5/thumbnails/15.jpg)
How to write a function for repetitive operations ? Repeated set of instructions
(curX, curY) = (nextX, nextY) (nextX, nextY) = randWalk.randNext(curX, curY) tt.goto(nextX, nextY)
Write a function, called drawRandom(), that performs the repeated set of instructions
![Page 16: Iteration “for ??? in range(min,max,interval):” What if we do not know the number of repetitions ? “while (???):” (p.49) problem = input(“Enter a math](https://reader036.vdocument.in/reader036/viewer/2022081603/56649f305503460f94c4b6f5/html5/thumbnails/16.jpg)
What is a potential problem ?
May run out of the display window How can we make it sure that turtle stays in bound
?
![Page 17: Iteration “for ??? in range(min,max,interval):” What if we do not know the number of repetitions ? “while (???):” (p.49) problem = input(“Enter a math](https://reader036.vdocument.in/reader036/viewer/2022081603/56649f305503460f94c4b6f5/html5/thumbnails/17.jpg)
Lab
Write a Python function of a drunken turtle which 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
Email your Python program [email protected]