Decision Structures, String Comparison, Nested Structures

Slides:



Advertisements
Similar presentations
Python - Selection Starter
Advertisements

Introduction to Computing Science and Programming I
Conditional Statements Introduction to Computing Science and Programming I.
James Tam Making Decisions In Python In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
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.
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.
Making Decisions In Python
From Scratch to Python Learn to program like the big boys / girls!
An Introduction to Textual 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.
Agenda Exam #1 Review Modulus Conditionals Boolean Algebra Reading: Chapter Homework #5.
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.
For loops in programming Assumes you have seen assignment statements and print statements.
COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON BRANCHES AND LOOPS Jehan-François Pâris
Python break,continue and pass Statements. The break Statement: for letter in 'Python': # First Example if letter == 'h': break print 'Current Letter.
Intro to Nested Looping Intro to Computer Science CS1510 Dr. Sarah Diesburg.
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
James Tam Making Decisions In Python In this section of notes you will learn how to have your programs choose between alternative courses of action.
Logical Operators, Boolean Variables, Random Numbers This template was just too good to let go in one day!
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 3 Decision Trees Conditionals.
Python Basics  Values, Types, Variables, Expressions  Assignments  I/O  Control Structures.
Visual Basic Declaring Variables Dim x as Integer = 0 In the statement above, x is being declared as an Integer (whole number) and is initialised.
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.
CMSC201 Computer Science I for Majors Lecture 07 – While Loops
Introduction to Decision Structures and Boolean Variables
GCSE COMPUTER SCIENCE Practical Programming using Python
Python - Iteration A FOR loop is ideal if we know how many times we want to repeat. Try this code: for loopCounter in range(10): print(loopCounter) There.
Week of 12/12/16 Test Review.
Line Continuation, Output Formatting, and Decision Structures
Lesson 04: Conditionals Topic: Introduction to Programming, Zybook Ch 3, P4E Ch 3. Slides on website.
Upsorn Praphamontripong CS 1110 Introduction to Programming Fall 2016
Selection and Python Syntax
IF statements.
Chapter 4: Control Structures
Lecture 07 More Repetition Richard Gesick.
Selection By Ramin && Taimoor
Writing Functions( ) (Part 5)
Decision Structures, String Comparison, Nested Structures
Line Continuation, Output Formatting, and Decision Structures
Selection CIS 40 – Introduction to Programming in Python
Learning to Program in Python
Decision Structures, String Comparison, Nested Structures
Exception Handling.
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
More Loops.
Intro to Nested Looping
Multiple Selections (ELIF Statements)
Practice with loops! What is the output of each function below?
Module 4 Loops.
More Looping Structures
3. Decision Structures Rocky K. C. Chang 19 September 2018
Introduction to Decision Structures and Boolean Variables
Writing Functions( ) (Part 4)
CS 1111 Introduction to Programming Spring 2019
Intro to Nested Looping
Python programming exercise
Boolean Expressions to Make Comparisons
Python Basics with Jupyter Notebook
CHAPTER 5: Control Flow Tools (if statement)
Conditionals and Loops
More Looping Structures
EE 194/BIO 196: Modeling biological systems
Python SAT 1 Feedback.
REPETITION Why Repetition?
Control Structures.
Flow Control I Branching and Looping.
Presentation transcript:

Decision Structures, String Comparison, Nested Structures Best Powerpoint Layout Ever!

Practice: Magic Number Game Rewrite the magic number game Set a magic number between 1 and 10 Ask the user to guess the number, but this time, if the guess is above the magic number, print out “too high!” and if the guess is below the magic number, print out “too low!”

Practice: Magic Number Game Additional: Now, write the program so that if the user inputs any number less than zero or greater than ten, print out a statement that says “That number is not in the given range.”

Nested Decision Structures Sometimes, one question isn’t enough. We may need to ask “follow-up” questions. Python allows us to nest decision structures inside one another, allowing you to evaluate additional conditions once a “higher” condition is satisfied In other words, we can have an “if” statement inside another “if” statement

Nested Decision Structures Remember, indentations are important in decision structures Make sure to indent accordingly for each decision structure

Practice: Magic Number Game magic = 5 guess = float(input (“Guess the magic number: ” )) if guess == magic: print (“Woah! You were right.”) else: if guess > magic: print(“Too high!”) print (“Too low!”)

Practice: Overtime Calculations Rewrite the overtime calculation program This time, account for if the user inputs any integers that don’t make sense Use nested structures to tell the user, they cannot work negative hours, or anything over 168 hours … or anything even close to that

ELIF There is one more reserved word for our decision structures We call it the “elif” statements The “elif” word allows you to check for an multiple conditions at a time This is different from checking additional conditions within one another

ELIF if guess == magic: print (“You got it!”) elif guess > magic: print (“Too high!”) else: print (“Too low!”)

Difference between IF-ELIF The difference is subtle but the “elif” and “if” function differently When you use the “elif” to check multiple conditions, Python will consider all conditions as part of a SINGLE decision structure. Therefore, the program will skip over any additional conditions after a single condition is met. The “if” conditions are checked always and every time.

Example num = int(input("Give me a number")) if num % 2 == 0: # this program only checks for print("Divisible by 2") the smallest prime factor of elif num % 3 == 0: the given number print("Divisible by 3") elif num % 5 == 0: print("Divisible by 5")

Example num = int(input("Give me a number")) if num % 2 == 0: # this program checks for ALL print("Divisible by 2") factors of the given number if num % 3 == 0: print("Divisible by 3") if num % 5 == 0: print("Divisible by 5")

Challenge: Grade Generator Write a program that asks the user for their grade on the last test they took If the user inputs a number greater than 100, or less than 0, tell them to put in a different number Then, according to the grade they give you, print out their letter grade

Challenge: Asian Standards 97<𝑔𝑟𝑎𝑑𝑒≤ 100 # A 92<𝑔𝑟𝑎𝑑𝑒≤ 97 # B 90<𝑔𝑟𝑎𝑑𝑒≤ 92 # C 85≤𝑔𝑟𝑎𝑑𝑒≤ 90 # D 𝑔𝑟𝑎𝑑𝑒< 85 # F