1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 3 Decision Trees Conditionals.

Slides:



Advertisements
Similar presentations
Lesson 6: Boolean and If Statements Computer Science 1 Mr. Bernstein.
Advertisements

If Statements, Try Catch and Validation. The main statement used in C# for making decisions depending on different conditions is called the If statement.
CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
ECS 15 if and random. Topic  Testing user input using if statements  Truth and falsehood in Python  Getting random numbers.
Making Decisions in Python Sec 9-10 Web Design. Objectives The student will: Understand how to make a decision in Python Understand the structure of an.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 4 Making Decisions in a Program.
Python Programming Language
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 …
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 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.
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.
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.
Computer Science 111 Fundamentals of Programming I Making Choices with if Statements.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
PROGRAMMING In Lesson 2. STARTER ACTIVITY Complete the starter activity in your python folder – lesson 2 Now we will see how you got on and update your.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Decision Making CMSC 201. Overview Today we will learn about: Boolean expressions Decision making.
Chapter 3: Branching and Program Flow CSCI-UA 0002 – Introduction to Computer Programming Mr. Joel Kemp.
COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON BRANCHES AND LOOPS Jehan-François Pâris
Python Selection. All the programs you have been developing so far have been sequential, this means that each instruction is executed in a set order.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 4 Conditionals Turtles.
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.
Decision Structures and Boolean Variables. Sequence Structures Thus far, we’ve been programming “sequence structures” Thus far, we’ve been programming.
Decision Structures, String Comparison, Nested Structures
Basic Conditions. Challenge: ● Ask the user his/her name ● If it’s “Wally,” jeer him ● Pause video and try on your own.
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers.
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
8. DECISION STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Decision Making CMSC 201 Chang (rev ).
Shell script – part 2 CS 302. Special shell variable $0.. $9  Positional parameters or command line arguments  For example, a script myscript take 2.
These Guys? Wait, What? Really?  Branching is a fundamental part of programming  It means taking an action based on decision  The decision is dependent.
If statement.  It is made up of three main components:  The keyword itself,  an expression that is tested for its truth value,  and a code suite to.
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!
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.
GCSE COMPUTER SCIENCE Practical Programming using Python Lesson 4 - Selection.
Introduction to Decision Structures and Boolean Variables
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
Python: Control Structures
IF statements.
Printing Lines of Asterisks
Topics The if Statement The if-else Statement Comparing Strings
If, else, elif.
Topics The if Statement The if-else Statement Comparing Strings
Line Continuation, Output Formatting, and Decision Structures
Selection CIS 40 – Introduction to Programming in Python
Python - Conditional Execution
Learning to Program in Python
Multiple Selections (ELIF Statements)
3. Decision Structures Rocky K. C. Chang 19 September 2018
Visual Basic – Decision Statements
Introduction to Programming Using Python PART 2
Selection Statements.
Introduction to Decision Structures and Boolean Variables
Topics The if Statement The if-else Statement Comparing Strings
PYTHON: BUILDING BLOCKS Sequencing & Selection
Programming Concepts and Database
CHAPTER 5: Control Flow Tools (if statement)
Control Structures.
Presentation transcript:

1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 3 Decision Trees Conditionals

2 Sequential Programming # Program: winter.py answer = raw_input("Do you like to ski? ") print "You said " + answer print "Great!" "Do you like to ski?" print "You said " + answer print "Great!" In sequential programming, each statement is executed in order. The program runs the same way every time it is run.

3 Decision Trees We would like to be able to respond to the user's input, giving different responses depending on what she enters: "Do you like to ski?" print "You will love winter here!"print "You should learn!" truefalse Decision trees diagram the different paths a program can take depending on a series of questions and answers. answer equals yes?

4 Conditional Statements # Program: skiReport.py answer = raw_input("Do you like to ski? ") if answer == "yes": print "You will love winter here!" else: print "Better learn!"

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

6 Conditions Conditions are expressions that have a value of true or false. A common type of condition is a comparison. Example of comparisons: x > y myName == "Jessica" 14 < size What are the variables in the above examples? Also can use, =, or != for comparisons.

7 Program Example # Program compare.py print "Type in two integers." x = input("First integer: ") y = input("Second integer: ") if x > y: print x, "is greater than", y else: print y, "is greater than or equal to", x What is the output if the user enters 4 and then 7?

8 Compound statements You can have as many separate statements as you like in the if or the else clauses. The statements must all have the same indentation. Together they form a block or a compound statement. answer = raw_input("Do you like to ski? ") if answer == "yes": print "You will love winter here!" print "We have lots of snow!" else: print "Better learn!" print "You will enjoy winter more!" print "Goodbye!"

9 Three way choices "Guess a Number: " print "Too low"guess > prizeNumber? print "Too high" truefalse print "You win!" guess < prizeNumber? true false

10 Using if.. elif.. else prizeNumber = 42 guess = input("Guess a number ") if guess < prizeNumber: print "Too low." elif guess > prizeNumber: print "Too high." else: print "You win!" Only one of the three blocks is executed.

11 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"?

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