if statements

If and else statments

'is equal to' symbol

colon required

must indent (tab key)

code = input("What is the secret code? ")

if code == "spongebob54":

          print("CORRECT!")

else: print("INCORRECT!") 

colon required

if and else must be in line

What is the secret code? vanillaicecream

INCORRECT

What is the secret code? spongebob54

CORRECT

Press F5 to run program:

if, elif and else statements

colon required

greater than or equal to

must indent (tab key)

temp = int(input("What is the temperature? "))

if temp >= 30:

          print("It is too hot!")

elif temp <= 15:

          print("It is too cold!")

else: print("It is just right.") 

else if

less than or equal to

uses whole numbers,

requires int input

must indent (tab key)

if, elif and else must be in line

What is the temperature? 11

It is too cold!

What is the temperature? 30

It is too hot!

What is the temperature? 24

It is just right.

Press F5 to run program:

Logical Operators

word = input("What is the secret word? ")

number = (int(input("What is the secret number? "))

if word == "grape" and number == 23:

          print("Correct! You may enter.")

elif word == "grape" or number == 23:

          print("You have one part correct.")

else: print("Incorrect! Please leave.") 

and - both parts must be true

or - one or more parts must be true

Press F5 to run program:

What is the secret word? grape

What is the secret number? 23

Correct! You may enter.

What is the secret word? cranberry

What is the secret number? 23

You have one part correct.

What is the secret word? strawberry

What is the secret number? 42

Incorrect! Please leave.

© CSNewbs 2020