COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON BRANCHES AND LOOPS Jehan-François Pâris

Slides:



Advertisements
Similar presentations
Summer 2012 Instructor: Hassan Khosravi
Advertisements

Lilian Blot CORE ELEMENTS SELECTION & FUNCTIONS Lecture 3 Autumn 2014 TPOP 1.
Franklin Township Elementary School Career Day: Computer Science
ECS 15 if and random. Topic  Testing user input using if statements  Truth and falsehood in Python  Getting random numbers.
While Loops. Challenge: ● Ask the user a simple math questions ● Continue asking the question until the user gets it right.
Selection (decision) control structure Learning objective
Top-Down Design CSC 161: The Art of Programming Prof. Henry Kautz 9/16/2009.
Μαθαίνοντας Python [Κ4] ‘Guess the Number’
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.
Introduction to Programming with Java, for Beginners Control Structures.
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/33 Conditionals and Loops Now we will examine programming statements.
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.
Noadswood Science,  To know the basics of Python coding and decoding Monday, September 07, 2015.
An Introduction to Textual Programming
Hands on Projects Dr. Bernard Chen Ph.D. University of Central Arkansas July 9 th 2012
Guide to Programming with Python Chapter Three Branching, while Loops, and Program Planning: The Guess My Number Game.
Python Control Flow statements There are three control flow statements in Python - if, for and while.
ECS 10 10/8. Outline Announcements Homework 2 questions Boolean expressions If/else statements State variables and avoiding sys.exit(…) Example: Coin.
Branching and Conditions CSIS 1595: Fundamentals of Programming and Problem Solving 1.
COSC 1306 COMPUTER SCIENCE AND PROGRAMMING Jehan-François Pâris
Module 3 Fraser High School. Module 3 – Loops and Booleans import statements - random use the random.randint() function use while loop write conditional.
If statements while loop for loop
Flow of Control Part 1: Selection
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.
Computer Science 111 Fundamentals of Programming I Making Choices with if Statements.
Course A201: Introduction to Programming 09/16/2010.
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.
Georgia Institute of Technology More on Creating Classes part 2 Barb Ericson Georgia Institute of Technology Oct 2005.
Intro to Nested Looping Intro to Computer Science CS1510 Dr. Sarah Diesburg.
CHAPTER 3 Decisions and Repetition. A few new constants Constants so far:  Strings  Numbers  integer  float Boolean constants: [On board]
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Decision Structures, String Comparison, Nested Structures
Designing While Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Logical Operators, Boolean Variables, Random Numbers This template was just too good to let go in one day!
Random UNPREDICTABLE NUMBERS. A FEW APPLICATIONS THAT USE RANDOM…. Predictions for life expectance used in insurance Business simulations Games (Give.
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers.
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)
Decision Making CMSC 201 Chang (rev ).
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 3 Decision Trees Conditionals.
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)
19 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Slide 1 Chapter 4 The If…Then Statement  Conditional control structure, also called a decision structure  Executes a set of statements when a condition.
Control Flow (Python) Dr. José M. Reyes Álamo.
CSc 110, Spring 2017 Lecture 12: Random Numbers
Control Structures II Chapter 3
Selection and Python Syntax
CSc 110, Autumn 2016 Lecture 13: Random Numbers
Writing Functions( ) (Part 5)
And now for something completely different . . .
Learning to Program in Python
Pick a number, any number …
Decision Structures, String Comparison, Nested Structures
Intro to Nested Looping
3. Decision Structures Rocky K. C. Chang 19 September 2018
Conditional and iterative statements
Intro to Nested Looping
Topics The if Statement The if-else Statement Comparing Strings
Console.WriteLine(“Good luck!”);
COSC 1306 COMPUTER SCIENCE AND PROGRAMMING
CHAPTER 5: Control Flow Tools (if statement)
Conditionals and Loops
Flow Control I Branching and Looping.
Presentation transcript:

COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON BRANCHES AND LOOPS Jehan-François Pâris

STARTING WITH A GAME

Number guessing game We use a random number generator to generate a random number between 1 and 10 Users are asked to guess the number until they come with the right value

Number guessing game We use a random number generator to generate a random number between 1 and 10 Users are asked to guess the number until they come with the right value

Flow chart Generate RN n You win Input i i == n TrueFalse

More about flow charts Graphic representation of flow of control –Loops represented by loops –Ifs have two branches ??? False True

Simple if If condition : something If tank_empty : get_gas condition False True something

FalseTrue If with else clause (I) If condition: something else: other stuff other stuff something condition

If with else clause (II) If hungry : Macdonald else : Starbucks In either case, you will leave the freeway

While something True False while condition : something while stain : keep_washing condition Go back!

How to generate RN Import a function from module random – from random import randint randint(min, max) –generates an integer between min and max

The program (I) # guess.py “”” must guess a random number between 1 and 10 “”” # secret # guess

The program (II) from random import randint secret = randint (1,10) guess = -1 # bad guess while guess != secret : guess = int(input("Enter your guess: ") print("You win!")

A better program Tells whether guess is too low or too high Must consider three cases inside the loop –Correct guess –Guess that is too low –Guess that is too high

The better program (I) # guess.py “”” must guess a random number between 1 and 10 “”” # secret # guess from random import randint secret = randint (1,10) guess = -1 # bad guess

The program (II) while guess != secret : guess = int(input(“Enter your guess: “)) if guess == secret : print ("You win!") else : if guess < secret : print ("Too low!") else: print ("Too high!")

Using an elif (I) Too many nested ifs if cond-1 : … else if cond-2 : … else if cond-3 : … else …

Example while guess != secret : guess = int(input("Enter your guess: ")) if guess == secret : print ("You win!") elif guess < secret : # observe indentation print ("Too low!") else: print ("Too high!")

Using an elif (II) With elif, lines align better if cond-1 : … elif cond-2 : … elif cond-3 : … else …

Indenting advice Python attaches great importance to indenting –You cannot indent anything that is not inside An if, a while, a for, … A function declaration or a main function –Your indentation must be consistent Do not mix spaces and tabs