#100DaysOfCode: Python Day 3

#100DaysOfCode: Python Day 3

Control Flow & Logical Operators

ยท

4 min read

#100DaysOfCode Day 3

๐Ÿช™๐Ÿดโ€โ˜ ๏ธ I CREATED MY OWN TREASURE HUNT GAME! ๐Ÿดโ€โ˜ ๏ธ๐Ÿช™
CLICK HERE and press "Run" to play.
Give this post a reaction if you loved the game and comment with how far you got.

So yesterday, I learned all about control flow and logical operators. I would like to believe that I am getting better at writing "well-commented code" for you to learn from, so I won't be saying much in these posts. The main focus here is enhancing the rollercoaster example.

Lessons

  1. Patience and creativity pay off.
  2. It may be a tutorial/walkthrough but feel free to change the print messages and have a little fun ๐Ÿคฃ.
  3. Why does it always have to be 3 lessons? It doesn't... - pretend this point is not here.

TO THE CODE!!!

controlFlow.py - if else, elif, if

# If statement

# if condition:
#     do this
# else:
#     do this

#-----------
# Example 1
water_level = 50
if water_level > 80:
    print("Drain water")
else:
    print("Continue")

#-----------
# Example 2
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("Sorry, you have to grow taller before you can ride.")

#-------------------------------------------------
# Nested If statements and elif (else + if combined)

# Nested if statement
# if condition:
#     if another condition:
#         do this
#     else:
#         do this
# else:
#     do this

# Example
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("What is your age?")
    if age <= 18:
        print("Please pay $7.")
    else:
        print("Please pay $12.")
else:
    print("Sorry, you have to grow taller before you can ride.")

#-----------
# elif - Only one thing will be carried out
# if condition1:
#     do A
# elif condition2:
#     do B
# elif:
#     do this

# Example
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("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.")

#-----------
# Multiply if - All conditions are checked and executed if true

# if condition1:
#     do A
# if condition2:
#     do B
# if condition3:
#     do C

# Example
print("Welcome to the rollercoaster!")
height = int(input("What is your height in cm? "))
bill = 0

if height >= 120:
    print("You can ride the rollercoaster!")
    age = int("What is your age?")
    if age < 12:
        bill = 5
        print("Child tickets are $5.")
    elif age <= 18:
        bill = 7
        print("Youth tickets are $7.")
    else:
        bill = 12
        print("Adult tickets are $12.")

    wants_photo = input("Do you want a photo taken? Y or N")
    if wants_photo == "Y":
        bill += 3

    print(f"Your final bill is ${bill}")

else:
    print("Sorry, you have to grow taller before you can ride.")

logicalOperators.py - and, or, not, >, <, >=, <=, ==

# Logical Operators
# if condition1 & condition2 & condition3:
#     do this
# else:
#     do this

# A and B 
# C or D
# not E

# Comparison Operators
# > is greater than
# < is less than
# >= is greater than or equal to
# >= is greater than or equal to
# == is equal to

# Example
print("Welcome to the rollercoaster!")
height = int(input("What is your height in cm? "))
bill = 0

if height >= 120:
    print("You can ride the rollercoaster!")
    age = int("What is your age?")
    if age < 12:
        bill = 5
        print("Child tickets are $5.")
    elif age <= 18:
        bill = 7
        print("Youth tickets are $7.")
    elif age >= 45 and age <= 55:
        print("Everything is going ot be okay. Have a free ride on us!")
    else:
        bill = 12
        print("Adult tickets are $12.")

    wants_photo = input("Do you want a photo taken? Y or N")
    if wants_photo == "Y":
        bill += 3

    print(f"Your final bill is ${bill}")

else:
    print("Sorry, you have to grow taller before you can ride.")

I MADE IT TO DAY 3. Let's see if this habit sticks for another 97 days.