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.

Slides:



Advertisements
Similar presentations
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 8 Fruitful Functions 05/02/09 Python Mini-Course: Day 2 - Lesson 8 1.
Advertisements

Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 4 Making Decisions in a Program.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
Python (yay!) November 16, Unit 7. Recap We can store values in variables using an assignment statement >>>x = We can get input from the user using.
Chapter 2 Writing Simple Programs
Geography 465 Assignments, Conditionals, and Loops.
INTRODUCTION TO PYTHON PART 3 - LOOPS AND CONDITIONAL LOGIC CSC482 Introduction to Text Analytics Thomas Tiahrt, MA, PhD.
Programming: Simple Control Structures Alice. Control Statements We have been using Do in order and Do together to control the way instructions are executed.
Python Control of Flow.
Simple Python Loops Sec 9-7 Web Design.
Fundamentals of Python: From First Programs Through Data Structures
Fundamentals of Python: First Programs
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
Line Continuation, Output Formatting, and Decision Structures CS303E: Elements of Computers and Programming.
ECS 10 10/8. Outline Announcements Homework 2 questions Boolean expressions If/else statements State variables and avoiding sys.exit(…) Example: Coin.
PYTHON CONDITIONALS AND RECURSION : CHAPTER 5 FROM THINK PYTHON HOW TO THINK LIKE A COMPUTER SCIENTIST.
Branching and Conditions CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Python – Making Decisions Lecture 02. Control Structures A program that only has one flow is useful but limited. We can use if statements to make these.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 6 Value-Returning.
Python Programming in Context Chapter 2. Objectives To understand how computers can help solve real problems To further explore numeric expressions, variables,
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
PAGES:51-59 SECTION: CONTROL1 : DECISIONS Decisions.
Programming: Simple Control Structures Alice. Control Statements We have been using Do in order and Do together to control the way instructions are executed.
Four Fundamental Pieces Instruction Control Structure Function Expression.
© Jalal Kawash Programming Peeking into Computer Science 1.
If..else Use random numbers to compute an approximation of pi Simulation of a special game of darts Randomly place darts on the board pi can be computed.
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
Mathematical Expressions, Conditional Statements, Control Structures
CSCI 161 Lecture 7 Martin van Bommel. Control Statements Statements that affect the sequence of execution of other statements Normal is sequential May.
Programming: Simple Control Structures
Obj: Programming: Simple Control Structures HW: Read section 3 – 2 AC3 D2 Do Now: 1.Log on to Alice. Open the file firstEncounter.a2w located in the folder.
Midterm Review Important control structures Functions Loops Conditionals Important things to review Binary Boolean operators (and, or, not) Libraries (import.
September 7, 2004ICP: Chapter 3: Control Structures1 Introduction to Computer Programming Chapter 3: Control Structures Michael Scherger Department of.
8. DECISION STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Programming: Simple Control Structures Sec 46 Web Design.
The If Statement There are no switch statements in Python. You need to use just if statements. There are no switch statements in Python. You need to use.
Xi Wang Yang Zhang. 1. Easy to learn 2. Clean and readable codes 3. A lot of useful packages, especially for web scraping and text mining 4. Growing popularity.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
IST 210: PHP LOGIC IST 210: Organization of Data IST210 1.
Loops (While and For) CSE 1310 – Introduction to Computers and Programming 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.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
Random Functions Selection Structure Comparison Operators Logical Operator
IF STATEMENTS AND BOOLEAN EXPRESSIONS. BOOLEAN EXPRESSIONS Evaluate to a value of true or false Use relational or equivalence operators Boolean operators.
PYTHON IF-STATEMENTS. What you know If something is true, then something happens Example If you heat water to 100 degrees Celsius, then it boils If it.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
Control Flow (Python) Dr. José M. Reyes Álamo.
For vs. While loop count=0 while count< 5: for count in range(5):
Programming: Simple Control Structures
turtle module Simple graphics programming How to use it ?
Programming: Simple Control Structures
Expressions and Control Flow in JavaScript
Programming: Simple Control Structures
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions (Part 2)
Programming: Simple Control Structures
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions (Part 2)
Chapter 8: More on the Repetition Structure
Programming: Simple Control Structures
Programming: Simple Control Structures
Computer Science Core Concepts
Types, Truth, and Expressions (Part 2)
Programming: Simple Control Structures
Relational Expressions
Types, Truth, and Expressions
Programming: Simple Control Structures
Types, Truth, and Expressions (Part 2)
Presentation transcript:

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

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

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

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

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

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]

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

Conditional Drunken turtle Random walk or Brownian motion A turtle From the current position Take an arbitrary direction and distance

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

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

Random Walk import random # # function randNext(curX, curY) # Given coordinate (curX, curY), generate next random coordinate # def randNext(curX, curY): nextX = curX *random.random() nextY = curY *random.random() return nextX, nextY

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

What is a potential problem ? May run out of the display window How can we make it sure that turtle stays in bound ?

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 your Python program