Presentation is loading. Please wait.

Presentation is loading. Please wait.

Iteration In Python new file, enter the following ‘for loop’ can be nested for a in range(3): for b in range(-2, 3): print(“ Inner loop: ”,a, b, a+b) print(“Outer.

Similar presentations


Presentation on theme: "Iteration In Python new file, enter the following ‘for loop’ can be nested for a in range(3): for b in range(-2, 3): print(“ Inner loop: ”,a, b, a+b) print(“Outer."— Presentation transcript:

1 Iteration In Python new file, enter the following ‘for loop’ can be nested for a in range(3): for b in range(-2, 3): print(“ Inner loop: ”,a, b, a+b) print(“Outer loop: “,a, b)

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 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: “)

3 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?”

4 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

5 Boolean Operators and, or, not 6<10 and 3<7 4!=4 or 5<8 not 6<10

6 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

7 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]

8 if (expression): next indented block else: Statements false Pictorial view of how if works true

9 Iteration Example Investment Current investment Investment+return at a year’s end Investment+return becomes the current investment at a new year’s beginning

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.random() returns a random fraction between 0 and 1 How to get a random number between (0,100)? A random number between (-50, 50) ?

12 Drunken Turtle Present location of turtle: curX, curY Next random location ? nextX = curX + 100* random.random() - 50 nextY = curY + 100* random.random() - 50

13 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

14

15

16

17 Lab_0224 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 Email your Python program 2016spring91.100@gmail.com


Download ppt "Iteration In Python new file, enter the following ‘for loop’ can be nested for a in range(3): for b in range(-2, 3): print(“ Inner loop: ”,a, b, a+b) print(“Outer."

Similar presentations


Ads by Google