Table of contents

  1. if / else
  2. Comparison Operators
  3. Nested if / else
  4. if / elif / else
  5. Multiple if
  6. Logical Operators
  7. Day 3 Project: Treasure Island
  8. 三元運算子

if / else

if condition:   
	do this (condition is True)
else:
	do this (condition is False)

下載.png

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

Comparison Operators

Untitled

#Exercise 3-1

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

Nested if / else

if condition:
	if another condition:
		do this
	else:
		do this
else:
	do this

Untitled

if height >= 120:
  print("You can ride the rollercoaster!")
  age = int(input("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.")

if / elif / else