Day 3–Python: Control Flow and Logical Operators

RESOURCE: draw.io for diagrams

Spacing very important in PYTHON

IF

ELSE

Need to indented as a pair

SYNTAX for IF and ELSE

if height >= 120:
— — — print(“You can ride the rollercoaster!”)
else:
— — — print(“Sorry, you have to grow taller before you can ride.”)

Modulo Operation %

Divide a number and give remainder 7 % 2 # 1

Challenge:

Create program that checks for odds and evens

number = int(input(“Which number do you want to check? “))

if number%2 == 0:

— — -print (“This is an even number.”)

else:

— — - print (“This is an odd number.”)

This is a binary solution so if its not even its odd. The modulo formula determines whether number has a remainder, as long it doesn’t have one, its even. If it does, its even.

Nested if statements and ELIF statements

print(“Welcome to the rollercoaster!”)
height = int(input(“What is your height in cm? “))
if height >= 120:
— — — print(“You can ride the rollercoaster!”)
age = int(input(“What is your age?”))
if age < 12:
— — — print(“Please pay $5.”)
elif age <= 18:
— — — print(“Please pay $7.”)
else:
— — — print(“Please pay $12”)
else:
— — — print(“Sorry, you have to grow taller before you can ride.”)

You can add as many ‘elif’ statements as you want to fit your condition.

You can add input within if statement as well.

Challenge: BMI Calculator Part 2

height = float(input(“enter your height in m: “))

weight = float(input(“enter your weight in kg: “))

Bmi = (weight/(height*height))

Bmi = (round(Bmi))

if Bmi <= 18.5:

— print (f”Your BMI is {Bmi}, you are underweight.”)

elif Bmi <= 25:

— print (f”Your BMI is {Bmi}, you have a normal weight.”)

elif Bmi <= 30:

— print (f”Your BMI is {Bmi}, you are slightly overweight.”)

elif Bmi <= 35:

— print (f”Your BMI is {Bmi}, you are obese.”)

else:

— print (f”Your BMI is {Bmi}, you are clinically obese.”)

Challenge: Pizza

The program worked but could have been more efficient if I used nested if statements properly — was a little confused with adding pepperoni with multiple conditionals.

I initially had this block of code but it didn’t pass the run test so i went the longer route just to get the challenge done. But this block could have replaced my 18–25 code block. 8 lines compared to instructors 5 lines. I also used the ‘AND’ operator — it hasn’t been taught yet i just googled it. Searched: If statements with multiple conditions python.

Logical Operators

And / Or / Not

Challenge: Love Calculator

You are going to write a program that tests the compatibility between two people.

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.

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**."

My Code for Love Calculator

Lines 9–10

My first step was to convert the user input into lowercase so its easier to count().

Lines 12–13

Created variables to store all the counted the numbers for both names in the word TRUE

Lines 16–17

Same thing for the word LOVE

Line 14 and 18

Converted both products to strings so i can concatenate for the ‘love formula’

Lines 21–22

Concatenated and then change back to integer for IF statements that require math.

Lines 24–29

Ifs and conditionals

Instructors Take

Angela Yu — Instructors Code

Some notes:

Wrap conditions in parenthesis for easier read

Project: BTS Game

Created a float chart to preface and brainstorm game:

My daughter Loves BTS

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store