Presentation is loading. Please wait.

Presentation is loading. Please wait.

Decision Structures, String Comparison, Nested Structures

Similar presentations


Presentation on theme: "Decision Structures, String Comparison, Nested Structures"— Presentation transcript:

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

2 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

3 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

4 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 … ”)

5 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 … ”)

6 The IF-ELSE Statement

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

8 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

9 ASCII Table

10 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

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

12 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.”

13 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

14 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!

15 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

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

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

18 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

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

20 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

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

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

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

24 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

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

26 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 …

27 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

28 Challenge: Asian Standards
95<𝑔𝑟𝑎𝑑𝑒≤ 100 # A 88<𝑔𝑟𝑎𝑑𝑒≤ 95 # B 80<𝑔𝑟𝑎𝑑𝑒≤ 88 # C 75≤𝑔𝑟𝑎𝑑𝑒≤ 80 # D 𝑔𝑟𝑎𝑑𝑒< 75 # F


Download ppt "Decision Structures, String Comparison, Nested Structures"

Similar presentations


Ads by Google