Day 3: Control Flow and Operators

Conditional Statements, Logical Operators, Code Blocks and Scope

Control Flow with if/else and Conditional Operators

Conditional Statement

if condition: do this else: do that water-level = 50 if water_level > 80: print("Drain Water") else: print("Continue")

Here's an example - Lets say you have a job at the theme-park and you have to write some code that will replace the ticket-box: Things to think about:-

  • Roller-coaster ride, kids need to be over 120cm

print ("Welcome to the Rollercoaster") height = int(input("What is your height in cm? ")) if height >= 120: print("You can ride the rollercoaster!") else: print(" You need to grow taller before you can ride. ") Symbols we can use (Comparison Operators)

OPERATOR

MEANING

>

Greater than

<

Less than

>=

Greater than or equal to

<=

Less than or equal to

==

Equal to (both sides are the same)

!=

Not equal to

Exercise - Odds or Evens

Nested if/else Statements

In the theme park ticket office scenario, we ALSO need to add in another condition, whereby if you are over 18 you pay $12 and if younger then you pay $7

In this case we use a nested if/else statement where we want MORE than 2 choices (as per the original if/else conditions) In a nested if/else statement, once the first condition has passed, we can check for another condition and then we can have another if/else statement

We find out that the code we have written hasnt taken into consideration that the under 12's pay junior rates of $5 and above 12 to 18 pay the $7.

Where we AGAIN had 2 options ($7 or $12) we used the if/else option. Here we have 3 options (or more) we can use the if / elif / else where with the elif options we can use as many as we want!!

The flow chart now looks like this:-

Exercise - Body Mass Calculator - v2.0 Upgrade

My effort :)

Angela's method of rounding did not include how many digits to round off to and left the function(round) by itself. She also did not use "and" like I did in the elif statements. All she did was say it had to be less that 25, less than 30 etc. Cleaner than mine :)

Exercise 2: Leap Year

Step by Step :)

Working from "How to know if it's a leap year" We know the facts as:

  • STEP 1

    • The data HAS to be divisible by 4 to be in contention as a leap year

  • STEP 2

    • At the moment the statement is TRUE, the date can be divided by 4. We now have to create another "IF" statement and see if it CAN be divided by 100.

  • STEP 3

    • If we CAN divide the date by 100 AS WELL as dividing it by 400, then its a leap year

    • So we create ANOTHER "if" statement

Multiple "IF" Statements

With the if /elif /else statement on the left, only ONE of the conditions will be carried out, whilst on the right ALL conditions will be checked and if it so happens that all 3 conditions are TRUE, all 3 will be carried out

Back to the Ticket Office : )

Going back to the ticket office we want to ask the people if they would like a photo of their ride, and if agreeable, then the $3 would be added onto the cost of admission

  • Lets look at the flowchart as it was BEFORE:

  • Lets look at the flowchart as it was AFTER:

This additional feature is completely independent of their age or height, this asks if they want a photo, a "yes" or a "no". If you do we will add on $3 to the existing ticket price To do this we will ask multiple "IF" conditions and checks if the 1st condition is "true" then do "A" , it will then move onto the 2nd condition and do "B" a and so on

So from the mind-map - how do we implement these extra cost for the photos?

Here is the original code with the new additions made

Exercise: Pizza

Logical Operators

We have done if statements, else statements, elif statements, multiple if statements as well as nested if statements. But we haven't be able to do thus far is to check for multiple conditions in the SAME LINE of code So how do we combine different conditions and say "Is the pizza large AND the user wants pepperoni AND extra cheese all in the same line of code? We use "Logical Operators" !! A and B C or D not E

AND:

When you combine 2 different conditions using an AND operator, they BOTH have to be TRUE for A and B for the entire line of code to be true. If ONE of them is FALSE, then the OVERALL thing evaluates to FALSE

OR:

When ONLY ONE of the conditions needs to be TRUE, then we use the OR operator If A OR B or if BOTH were TRUE - it would evaluate to TRUE If BOTH A and B are FALSE - will it evaluate to FALSE

NOT:

This operator basically REVERSES a condition. So, if a condition is FALSE, then it becomes TRUE, or of its TRUE, it becomes FALSE

Exercise - Rollercoaster + Logical Operators

Lets say that the theme park decided to give people between 45 and 55 free rides as they might be suffering from a mid-life crisis

How would we re-write our code that we have already:

Just a variation on the initial one above including free photos :)

Exercise: Love Calculator (Difficult)

Instructions

You are going to write a program that tests the compatibility between two people. We're going to use the super scientific method recommended by Buzz Feed.

To work out the love score between two people:

Take both people's names and check for the number of times the letters in the word TRUE occurs. Then check for the number of times the letters in the word LOVE occurs. Then combine these numbers to make a 2 digit number.

This video gives you more details on this:

For Love Scores less than 10 or greater than 90, the message should be:

"Your score is **x**, you go together like coke and mentos."

For Love Scores between 40 and 50, the message should be:

"Your score is **y**, you are alright together."

Otherwise, the message will just be their score. e.g.:

"Your score is **z**."

THINGS TO KNOW BEFORE WE START

  • We need to use the "lower function" - this changes all letters to lower case

  • Use .lower() - For example:

    • s = "Kilometer" ----------- in comma because its STRING :) print(s.lower())

  • We need to use the "count function" - this counts how many times a character appears in a string

  • Use .count( ) - For example

    • sentence = 'Mary had a little lamb'

      sentence.count('a") 4

My Answer

Really pleased I got there and very similar to Angela's solution which is a bonus :)

UPDATE: - Because I can :)

Day 3 Project - Treasure Hunt

ASCII Art - Cool art pictures you can have in your code. Dont forget that if you want to print a string across multiple lines use the 3 single quotes at the beginning and end print(' ' ' sjpjpjwpfjpwjepf etc ' ' ')

Last updated

Was this helpful?