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

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

4 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

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

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

7 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

8 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

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

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

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

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

13 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

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


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

Similar presentations


Ads by Google