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

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

Embed Size (px)

Citation preview

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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]