1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 4 Conditionals Turtles.

Slides:



Advertisements
Similar presentations
The upper-left corner of a graphics window has the location of: A.0,0 B.200,0 C.0,200 D.200,200.
Advertisements

Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Summer Computing Workshop. Introduction to Variables Variables are used in every aspect of programming. They are used to store data the programmer needs.
Noadswood Science,  To understand the flow procedure when writing programs Thursday, January 15, 2015.
Selection Flow Charts If statements. Flow of Control The flow of control is a concept we’ve already encountered. The concept of control relates to the.
 Control structures  Algorithm & flowchart  If statements  While statements.
Computer Science 1000 LOGO I. LOGO a computer programming language, typically used for education an old language (1967) the basics are simple: move a.
Conditional Statements Introduction to Computing Science and Programming I.
Overview Program flow Decision / branching / selection structures True & False in Python Comparison operators & Boolean expressions if … if … else if …
Week 5 while loops; logic; random numbers; tuples Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except.
CIS101 Introduction to Computing Week 12 Spring 2004.
Computer Science 1620 Selection Structures. Write a program that accepts the speed of a vehicle, and determines whether that person is speeding assume.
Intro to Robots Conditionals and Recursion. Intro to Robots Modulus Two integer division operators - / and %. When dividing an integer by an integer we.
Yr 9 a computer science Nice to meet you variable challenge! 1.Variable assessment Looping in python 2.Looping assessment Drawing in python using turtle.
Chapter 9 IF Statement Bernard Chen. If Statement The main statement used for selecting from alternative actions based on test results It’s the primary.
Chapter 9 IF Statement Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012.
Line Continuation, Output Formatting, and Decision Structures CS303E: Elements of Computers and Programming.
1 Survey of Computer Science CSCI 110, Spring 2011 Lecture 14 Recursion.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Decision Structures and Boolean Logic
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.
CS4311 Spring 2011 Unit Testing Dr. Guoqiang Hu Department of Computer Science UTEP.
Programming Training kiddo Main Points: - Python Statements - Problems with selections.
B.A. (Mahayana Studies) Introduction to Computer Science November March Logo (Part 2) More complex procedures using parameters,
End Show Writing a computer program involves performing the following tasks. 1. Understanding the problem 2. Developing an Algorithm for the problem 3.
Computer Science 111 Fundamentals of Programming I Introduction to Graphics.
Agent P, I have been hearing some rumours about a Python Turtle.
1 Turtle Graphics and Math Functions And how you can use them to draw cool pictures!
MSW Logo By Awin 9s.
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 18 What are programs for? Classes and Objects.
Lecture 4: C/C++ Control Structures Computer Programming Control Structures Lecture No. 4.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Lecture 17 Parameters, Scope, Return values.
Agenda Basic Logic Purpose if statement if / else statement
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Python 101 Dr. Bernard Chen University of Central Arkansas PyArkansas.
Summer Computing Workshop. Session 3 Conditional Branching  Conditional branching is used to alter the normal flow of execution depending on the value.
More Python!. Lists, Variables with more than one value Variables can point to more than one value at a time. The simplest way to do this is with a List.
Inequalities and their Graphs Symbols SymbolMeaningGraph < Less thanOpen circle > Greater thanOpen circle ≤ Less than or equal to Closed circle.
Selection Flow Charts If statements. Flow of Control The flow of control is a concept with which we’re already familiar. The concept of control relates.
1 Computer Graphics and Games MONT 105S, Spring 2009 Lecture 2 Simple Python Programs Working with numbers and strings.
8. DECISION STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 3 Decision Trees Conditionals.
Programming Training kiddo Main Points: - Python Statements - Problems with selections.
These Guys? Wait, What? Really?  Branching is a fundamental part of programming  It means taking an action based on decision  The decision is dependent.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Turtle Graphics Let’s see what we can draw on Python!
Turtle Graphics Lesson 2 1. There are 3 homeworks to complete during the six lessons of this unit. Your teacher will let you know when a homework has.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
PH2150 Scientific Computing Skills Control Structures in Python In general, statements are executed sequentially, top to bottom. There are many instances.
Control Flow (Python) Dr. José M. Reyes Álamo.
GCSE COMPUTER SCIENCE Practical Programming using Python
Line Continuation, Output Formatting, and Decision Structures
Making Choices with if Statements
General Form for a Conditional
Selection and Python Syntax
Printing Lines of Asterisks
Topics The if Statement The if-else Statement Comparing Strings
Topics The if Statement The if-else Statement Comparing Strings
Line Continuation, Output Formatting, and Decision Structures
Practice with loops! What is the output of each function below?
Selection Statements.
CS 1111 Introduction to Programming Spring 2019
Topics The if Statement The if-else Statement Comparing Strings
Programming Concepts and Database
Control Statements.
Python While Loops.
Presentation transcript:

1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 4 Conditionals Turtles

2 General Form for a Conditional if condition:# condition is true or false Python code A # Executed if condition is true else: Python code B # Executed if condition is false Only one of the two blocks of code is executed.

3 General Form for using elif in a Conditional if condition1:# condition is true or false Python code A # Executed if condition1 is true elif condition2: Python code B #Executed if condition1 is false and 2 is true else: Python code C # Executed if conditions 1 and 2 are false Only one of the blocks of code is executed.

4 Example Decision Tree "Enter your height: " print "Tall" height >= 70? print "Above average" truefalse print "Short" height >= 75? true false

5 An Alternate Decision Tree for if..elif..else "Enter your height: " print "Tall" print "Above average" print "Short" height >= 75height >= 70 Otherwise (height < 70) Conditions are printed next to the branches. Paths are tested one at a time from left to right. First condition that is true is the path that is followed. Only one path is executed.

6 Program Example # Program height.py height = input("Enter your height in inches: ") if height >= 75: print "Wow! You are tall!" print "Please join our basketball team." elif height >= 70: print "You are above average height." else: print "You are shorter than average." print "Thank you for participating in our survey."

7 Multiple decisions "Do you like to ski?" "Downhill or cross country?""You should learn!" true false truefalse "Try Wachusett""Try the local trails" In Python, we use a nested conditional to program this decision tree. skiAns equals "yes"? response equals "downhill"?

8 # Program: downhill.py skiAns = raw_input("Do you like to ski? ") if skiAns == "yes": response = raw_input("Downhill or cross country? ") if response == "downhill": print "Try Wachusett!" else: print "Try the local trails." else: print "You should learn!" Nested Conditional Example

9 Recall Classes and Objects Python is an object oriented programming language. All items of data are objects. Objects have properties: Things that describe the objects Objects have methods: Things an object can do. A class describes a group of objects that have the same methods and set of properties. Example: A car class Properties: color, number of doors, body type Methods: accelerate, brake, steer My car is an object that is a member of the car class.

10 The Turtle Class A turtle in python is an object from the Turtle class. A turtle can move around a graphics window. A turtle has a pen that can be up or down. If the pen is down, the turtle draws as it moves. Turtle properties: x position y position direction (that it's facing) pen position (up or down) pen color

11 Turtle Methods Turtle methods: forward(distance)Move forward pixels backward(distance)Move backward pixels right(angle)Turn right degrees left(angle)Turn left degrees up( )Lift pen up down( )Put Pen down goto(x, y)Move to position x, y circle(radius)Draw a circle with a given radius.

12 Turtle Example #Use a turtle to draw a triangle. from turtle import Pen as Turtle#import from library yertle = Turtle( )#Create a new Turtle object; name it yertle yertle.forward(100)#Move yertle forward by 100 pixels yertle.left(120)#Turn yertle left by 120 degrees yertle.forward(100)#Move yertle forward by 100 pixels yertle.left(120)#Turn yertle left by 120 degrees yertle.forward(100)#Move yertle forward by 100 pixels yertle.left(120)#Turn yertle left by 120 degrees