Skip to content

Latest commit

 

History

History
155 lines (138 loc) · 2.21 KB

lecture1.md

File metadata and controls

155 lines (138 loc) · 2.21 KB

CS50P W1 - Conditionals

if Statements

if x < y: 
    …
elif x > y:
    …
else : 
    …
if x < y or x > y:
	…
else:
    …
if x != y:
    …
else:
    …
if score >= 90 and score <= 100:
	…
if 90 <= score <= 100:
    …
if score >= 90:
    …

Modulo Operator %

Remainder of dividing two numbers not evenly

4 % 2 = 0 (divides evenly)
3 % 2 = 1 (dividing 3/2 = 1.5, the quotient is 1 and the remainder 0.5 x 2 = 1)
16 % 5 = 1 (dividing 16/5 = 3.2, the quotient is 3 and the remainder 0.2 x 5 = 1)
if x % 2 == 0:
	print("Even")
else:
    print("Odd")

bool

def is_even(n):
    if n % 2 == 0:
 		return True
	else:
      	return False

Pythonic syntax

def is_even(n):
    return True if n % 2 == 0 else False

def is_even(n):
    return n % 2 == 0		# The operation automatically returns a bool

# Integrating `is_even()`
if is_even(x):
	print("Even")

match Statements


name = input("What’s your name? ")

if name == "Harry": 
	print("Gryffindor")
elif name == "Harry": 
	print("Gryffindor")
elif name == "Harry": 
	print("Gryffindor")
elif name == "Draco":
    print("Slytherin")
else : 
	print("Who?")

name = input("What's your name? ")

if name == "Harry" or "Hermione" or "Draco": 
	print("Gryffindor")
elif name == "Draco":
    print("Slytherin")
else : 
	print("Who?")

  1. match
name = input("What’s your name? ")

match name:
case "Harry": 
	print("Gryffindor")
 caseHermione”: 
	print("Gryffindor")
caseRon”: 
	print("Gryffindor")
caseDraco”:
    print("Slytherin")
case _:
    print("Who?")

match name:
    case "Harry" | "Hermione" | "Ron":
		print("Gryffindor")
    case "Draco":
        print("Slytherin")
    case _:
        print("Who?")

Error Checking

def main():
	difficulty = ("Enter level: ")
	if not (difficulty == "Difficult" or difficulty == "Casual"):
		print("Enter a valid difficulty")
		return

if difficulty == "Difficult":
	if players == "Multiplayer":
		recommend("Poker")

if difficulty == "Difficult" and players == "Multiplayer":
	recommend("Poker")