Decision Structures, String Comparison, Nested Structures

Slides:



Advertisements
Similar presentations
CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Advertisements

This Week More Types boolean string Modules print statement Writing programs if statement Type boolean.
ECS 15 if and random. Topic  Testing user input using if statements  Truth and falsehood in Python  Getting random numbers.
Μαθαίνοντας Python [Κ4] ‘Guess the Number’
Conditional Statements Introduction to Computing Science and Programming I.
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.
School of Computing Science CMT1000 Ed Currie © Middlesex University 1 CMT1000: Introduction to Programming Ed Currie Lecture 5B: Branch Statements - Making.
Logical Operators and Conditional statements
Geography 465 Assignments, Conditionals, and Loops.
An Introduction to Textual Programming
Line Continuation, Output Formatting, and Decision Structures CS303E: Elements of Computers and Programming.
A453 Exemplar Password Program using VBA
CS0004: Introduction to Programming Relational Operators, Logical Operators, and If Statements.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
Decision Structures and Boolean Logic
Python Lesson Week 01. What we will accomplish today Install Python on your computer Using IDLE interactively to explore String Variables if/else while.
STRINGS CMSC 201 – Lab 3. Overview Objectives for today's lab:  Obtain experience using strings in Python, including looping over characters in strings.
Agenda Exam #1 Review Modulus Conditionals Boolean Algebra Reading: Chapter Homework #5.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
CONTENTS Processing structures and commands Control structures – Sequence Sequence – Selection Selection – Iteration Iteration Naming conventions – File.
Chapter 3: Branching and Program Flow CSCI-UA 0002 – Introduction to Computer Programming Mr. Joel Kemp.
Decision Structures and Boolean Variables. Sequence Structures Thus far, we’ve been programming “sequence structures” Thus far, we’ve been programming.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Flow of Control Unless indicated otherwise, the order of statement execution through a method is linear: one after the other in the order they are written.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Logical Operators, Boolean Variables, Random Numbers This template was just too good to let go in one day!
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.
Last Week Modules Save functions to a file, e.g., filename.py The file filename.py is a module We can use the functions in filename.py by importing it.
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
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.
Complex Branching Statements CSIS 1595: Fundamentals of Programming and Problem Solving 1.
These Guys? Wait, What? Really?  Branching is a fundamental part of programming  It means taking an action based on decision  The decision is dependent.
Exception Handling and String Manipulation. Exceptions An exception is an error that causes a program to halt while it’s running In other words, it something.
Introduction to Programming Python Lab 7: if Statement 19 February PythonLab7 lecture slides.ppt Ping Brennan
TUTORIAL 4 Visual Basic 6.0 Mr. Crone. Pseudocode Pseudocode is written language that is part-code part- English and helps a programmer to plan the layout.
CS 115 Lecture 8 Conditionals, if statements Taken from notes by Dr. Neil Moore.
 Type Called bool  Bool has only two possible values: True and False.
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
Introduction to Decision Structures and Boolean Variables
Introduction to Python
Line Continuation, Output Formatting, and Decision Structures
Selection and Python Syntax
IF statements.
Introduction to Programming
Topics The if Statement The if-else Statement Comparing Strings
Engineering Innovation Center
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Decision Structures, String Comparison, Nested Structures
Topics The if Statement The if-else Statement Comparing Strings
Line Continuation, Output Formatting, and Decision Structures
Introduction to Programming
And now for something completely different . . .
Decision Structures, String Comparison, Nested Structures
Decision Structures, String Comparison, Nested Structures
Introduction to Programming
Selection Statements.
Introduction to Decision Structures and Boolean Variables
Writing Functions( ) (Part 4)
Introduction to Computer Science
CHAPTER 5: Control Flow Tools (if statement)
Introduction to Programming
Lecture 7 – Unit 1 – Chatbots Python – For loops + Robustness
Presentation transcript:

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

The IF-ELSE Statement Previously, we wrote “IF” statements to check for a condition, and our programs followed the order of the program according to whether the conditions held “True” or “False” Now, we will add in another branch of execution based on the value of the Boolean expression

The IF-ELSE Statement We can do this by adding the reserved word “else” This tells the program to check for the “if” condition and if it is not satisfied, run the code branched under the “else” statement You need to add a colon at the end of each else statement

The IF-ELSE Statement Previously, we had to use two “if” statements like this: if temperature < 32: print(“It’s freezing outside”) if temperature >= 32: print(“It’s a normal day in October … ”)

The IF-ELSE Statement Now, with the “else”, we can write our codes like this: if temperature < 32: print(“It’s freezing outside”) else: print(“It’s a normal day in October … ”)

The IF-ELSE Statement

The IF-ELSE Statement hours = float(input(“how many hours did you work?”)) rate = float(input(“what’s your hourly rate?”)) if hours > 40: print(“Your total is”, (40*rate) + ( (hours - 40)*1.5*rate) ) else: print(“Your total is”, hours*rate)

String Comparison So far, we’ve been comparing data as numeric types We can also compare strings The way we do this is to convert all characters back to their basic data type, all one’s and zero’s using the ASCII/UNICODE table

ASCII Table

String Comparison When comparing strings, Python will compare one letter at a time, in the order that they appear Example: “animal” < “banana” # True Because the letter “a” in animal has a lower ASCII value than the letter “b” in banana, this Boolean expression holds True

String Comparison “dog” > “cat” # is “dog” greater than “cat”? “fish” < “alligator” # is “fish” less than “alligator”? “elephant” == “tiger” # are “elephant” and “tiger” equal? “Mr. Seok” != “Donald # are these different strings?

Practice: Passwords Write a program that asks the user to input a new password and store it in a variable Then, ask the user to confirm the password they just typed If they are exactly the same, then print out “Great! Password was saved!” Otherwise, print out “Sorry, the two passwords did not match.”

Basic String Manipulation Python has a huge list of string manipulation commands written in a package. We will take a look at many of them, but for now we’ll take a look at: lower() # this changes all characters into lower case upper() # this one changes into upper case

Basic String Manipulation These two commands are not actually built into Python’s function library, therefore we need to refer to a separate module We call this the “str” module Examples: string_lower = str.lower (“Hello, World!”) # hello, world! string_upper = str.upper (“Hello, World!”) # HELLO, WORLD!

Basic String Manipulation A module is a file containing functions defined in Python and other statements string_lower = str.lower (“Hello, World!”) The characters “ str. ” calls on a module entitled “str” that Python can access but is not directly built into the shell The word “lower()” calls the function within that specific module

Practice: Passwords Rewrite your password program so that it is not case sensitive Such that, “password” and “PaSsWoRd” are considered valid entries

Practice: Alphabetizing Strings Write a program that asks the user to input two names Then print back these names in alphabetical order

String Length Python also has a function that counts the number of characters in a string We call it the len() function and it returns an integer Example: count = len(“Donald”) # count will hold value 6

Practice: Compare Size of Strings Write a program that asks the user for two names Sort them in size order and print them out

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

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 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!”)

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

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!”)

Practice: Alphabetizing Strings Now, try writing a program that asks the user to input three names Then print these names back in alphabetical order You should also try four and so on and so forth …

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 95<𝑔𝑟𝑎𝑑𝑒≤ 100 # A 88<𝑔𝑟𝑎𝑑𝑒≤ 95 # B 80<𝑔𝑟𝑎𝑑𝑒≤ 88 # C 75≤𝑔𝑟𝑎𝑑𝑒≤ 80 # D 𝑔𝑟𝑎𝑑𝑒< 75 # F