[220] conditionals · 2020. 2. 10. · learning objectives today reason about conditionals...

58
[220] Conditionals Meena Syamkumar Mike Doescher Cheaters caught: 0 Piazza Enrollment 431 / 445 Exam Conflict From is available on the website

Upload: others

Post on 24-Jan-2021

45 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: [220] Conditionals · 2020. 2. 10. · Learning Objectives Today Reason about conditionals •Conditional execution •Alternate execution •Chained conditionals •Nested conditionals

[220] ConditionalsMeena Syamkumar

Mike Doescher

Cheaters caught: 0Piazza Enrollment 431 /

445

Exam Conflict From is available on the website

Page 2: [220] Conditionals · 2020. 2. 10. · Learning Objectives Today Reason about conditionals •Conditional execution •Alternate execution •Chained conditionals •Nested conditionals

Mental Model of Control Flow

x = 5y = f(x)return y+1

1. do statements in order, one at a time

2. functions: jump in and out of these

3. conditionals: sometimes skip statements

4. loops: sometimes go back to previous

threeexceptions

TODAY

...x = 5y = f(x)return y+1...

Code:

Page 3: [220] Conditionals · 2020. 2. 10. · Learning Objectives Today Reason about conditionals •Conditional execution •Alternate execution •Chained conditionals •Nested conditionals

Learning Objectives Today

Reason about conditionals• Conditional execution• Alternate execution• Chained conditionals• Nested conditionals

Understand code blocks• Be able to identify the lines of code in the same block

Sanity checking• Recognize errors• Sanitize bad data automatically

Chapter 5 of Think Python(skip "Recursion" sections)

Do PythonTutor Practice!(posted on schedule)

Page 4: [220] Conditionals · 2020. 2. 10. · Learning Objectives Today Reason about conditionals •Conditional execution •Alternate execution •Chained conditionals •Nested conditionals

Today's Outline

Review

Control Flow Diagrams

Basic syntax for “if”

Identifying code blocks

Demos

Page 5: [220] Conditionals · 2020. 2. 10. · Learning Objectives Today Reason about conditionals •Conditional execution •Alternate execution •Chained conditionals •Nested conditionals

Review 1: Indentation Example

print(“A”)print(“B”)

def print_letters():print(“C”)print(“D”)

print(“E”)print(“F”)

print_letters()

what does it print?

Page 6: [220] Conditionals · 2020. 2. 10. · Learning Objectives Today Reason about conditionals •Conditional execution •Alternate execution •Chained conditionals •Nested conditionals

Review 1: Indentation Example

print(“A”)print(“B”)

def print_letters():print(“C”)print(“D”)

print(“E”)print(“F”)

print_letters()

what does it print?

ABEFCD

Page 7: [220] Conditionals · 2020. 2. 10. · Learning Objectives Today Reason about conditionals •Conditional execution •Alternate execution •Chained conditionals •Nested conditionals

Review 1: Indentation Example

print(“A”)print(“B”)

def print_letters():print(“C”)print(“D”)

print(“E”)print(“F”)

print_letters()

what does it print?

indented, so “inside”print_letters function

ABEFCD

Page 8: [220] Conditionals · 2020. 2. 10. · Learning Objectives Today Reason about conditionals •Conditional execution •Alternate execution •Chained conditionals •Nested conditionals

Review 1: Indentation Example

print(“A”)print(“B”)

def print_letters():print(“C”)print(“D”)

print(“E”)print(“F”)

print_letters()

what does it print?

indented, so “inside”print_letters function

printed last becauseprint_letters is called last

ABEFCD

Page 9: [220] Conditionals · 2020. 2. 10. · Learning Objectives Today Reason about conditionals •Conditional execution •Alternate execution •Chained conditionals •Nested conditionals

Review 1: Indentation Example

print(“A”)print(“B”)

def print_letters():print(“C”)print(“D”)

print(“E”)print(“F”)

print_letters()

what does it print?

indented, so “inside”print_letters function

ABEFCD

Page 10: [220] Conditionals · 2020. 2. 10. · Learning Objectives Today Reason about conditionals •Conditional execution •Alternate execution •Chained conditionals •Nested conditionals

Review 1: Indentation Example

print(“A”)print(“B”)

def print_letters():print(“C”)print(“D”)

print(“E”)print(“F”)

print_letters()

what does it print?

indented, so “inside”print_letters function

not indented, so“outside” any function A

BEFCD

Page 11: [220] Conditionals · 2020. 2. 10. · Learning Objectives Today Reason about conditionals •Conditional execution •Alternate execution •Chained conditionals •Nested conditionals

Review 1: Indentation Example

print(“A”)print(“B”)

def print_letters():print(“C”)print(“D”)

print(“E”)print(“F”)

print_letters()

what does it print?

indented, so “inside”print_letters function

not indented, so“outside” any function

also not indented, so“outside” any function.

Runs BEFOREprint_letters is called

ABEFCD

Page 12: [220] Conditionals · 2020. 2. 10. · Learning Objectives Today Reason about conditionals •Conditional execution •Alternate execution •Chained conditionals •Nested conditionals

Review 1: Indentation Example

print(“A”)print(“B”)

def print_letters():print(“C”)print(“D”)

print(“E”)print(“F”)

print_letters()

what does it print?

indented, so “inside”print_letters function

not indented, so“outside” any function

also not indented, so“outside” any function.

Runs BEFOREprint_letters is called

We use indenting to tell Python which code is inside or outsideof a function (or other things we’ll learn about soon).

blank lines are irrelevant

ABEFCD

Page 13: [220] Conditionals · 2020. 2. 10. · Learning Objectives Today Reason about conditionals •Conditional execution •Alternate execution •Chained conditionals •Nested conditionals

Review 1: Indentation Example

print(“A”)print(“B”)

def print_letters():print(“C”)print(“D”)

print(“E”)print(“F”)

print_letters()

what does it print?

we’ll often call the linesof code inside something

a “block” of code

ABEFCD

Page 14: [220] Conditionals · 2020. 2. 10. · Learning Objectives Today Reason about conditionals •Conditional execution •Alternate execution •Chained conditionals •Nested conditionals

Review 1: Indentation Example

print(“A”)print(“B”)

def print_letters():print(“C”)

print(“D”)print(“E”)print(“F”)

print_letters()

what does it print?

horizontal spacesidentify blocks

(not vertical space)

ABEFCD

Page 15: [220] Conditionals · 2020. 2. 10. · Learning Objectives Today Reason about conditionals •Conditional execution •Alternate execution •Chained conditionals •Nested conditionals

Review 2: Argument Passing

def h(x=1, y=2):print(x, y) # what is printed?

def g(x, y):print(x, y) # what is printed?h(y)

def f(x, y):print(x, y) # what is printed?g(x=x, y=y+1)

x = 10y = 20f(y, x)

Page 16: [220] Conditionals · 2020. 2. 10. · Learning Objectives Today Reason about conditionals •Conditional execution •Alternate execution •Chained conditionals •Nested conditionals

Today's Outline

Review

Control Flow Diagrams

Basic syntax for “if”

Identifying code blocks

Demos

Page 17: [220] Conditionals · 2020. 2. 10. · Learning Objectives Today Reason about conditionals •Conditional execution •Alternate execution •Chained conditionals •Nested conditionals

https://www.drphysics.com/prayer/flowchart.html

conditional execution alternate execution

Page 18: [220] Conditionals · 2020. 2. 10. · Learning Objectives Today Reason about conditionals •Conditional execution •Alternate execution •Chained conditionals •Nested conditionals

https://www.drphysics.com/prayer/flowchart.html

conditional execution alternate execution

nestedconditional

chained would beif we had more than

two possibilities

Page 19: [220] Conditionals · 2020. 2. 10. · Learning Objectives Today Reason about conditionals •Conditional execution •Alternate execution •Chained conditionals •Nested conditionals

https://www.drphysics.com/prayer/flowchart.html

in programming:• questions are phrased as boolean expressions• actions are code/statements

conditional execution alternate execution

nestedconditional

chained would beif we had more than

two possibilities

Page 20: [220] Conditionals · 2020. 2. 10. · Learning Objectives Today Reason about conditionals •Conditional execution •Alternate execution •Chained conditionals •Nested conditionals

Control Flow Diagrams (Flowcharts for Code)

x = input(“enter x: ”)x = int(x)

x % 2 == 0

print(“it’s even”) print(“it’s odd”)

print(“thank you”)

FalseTrue

Page 21: [220] Conditionals · 2020. 2. 10. · Learning Objectives Today Reason about conditionals •Conditional execution •Alternate execution •Chained conditionals •Nested conditionals

Control Flow Diagrams (Flowcharts for Code)

print(“thank you”)

Sometimeswe do this

Other timeswe do this

condition

x % 2 == 0

print(“it’s even”) print(“it’s odd”)

FalseTrue

x = input(“enter x: ”)x = int(x)

Page 22: [220] Conditionals · 2020. 2. 10. · Learning Objectives Today Reason about conditionals •Conditional execution •Alternate execution •Chained conditionals •Nested conditionals

x % 2 == 0

print(“it’s even”) print(“it’s odd”)

FalseTrue

Control Flow Diagrams (Flowcharts for Code)

print(“thank you”)

Sometimeswe do this

Other timeswe do this

boolean expressions are mostlyused for deciding what to do next(not for printing “True” or “False”

as in most of our examples thus far)

condition

x = input(“enter x: ”)x = int(x)

Page 23: [220] Conditionals · 2020. 2. 10. · Learning Objectives Today Reason about conditionals •Conditional execution •Alternate execution •Chained conditionals •Nested conditionals

x % 2 == 0

print(“it’s even”) print(“it’s odd”)

FalseTrue

Control Flow Diagrams (Flowcharts for Code)

print(“thank you”)

Sometimeswe do this

Other timeswe do this

boolean expressions are mostlyused for deciding what to do next(not for printing “True” or “False”

as in most of our examples thus far)

condition

conditional

x = input(“enter x: ”)x = int(x)

Page 24: [220] Conditionals · 2020. 2. 10. · Learning Objectives Today Reason about conditionals •Conditional execution •Alternate execution •Chained conditionals •Nested conditionals

Branches (aka "Paths of Execution”)

enter x: 8it’s eventhank you

Input/Output:

x % 2 == 0

print(“it’s even”) print(“it’s odd”)

FalseTrue

print(“thank you”)

x = input(“enter x: ”)x = int(x)

Page 25: [220] Conditionals · 2020. 2. 10. · Learning Objectives Today Reason about conditionals •Conditional execution •Alternate execution •Chained conditionals •Nested conditionals

Branches (aka "Paths of Execution”)

enter x: 7it’s oddthank you

Input/Output:

x % 2 == 0

print(“it’s even”) print(“it’s odd”)

FalseTrue

print(“thank you”)

x = input(“enter x: ”)x = int(x)

Page 26: [220] Conditionals · 2020. 2. 10. · Learning Objectives Today Reason about conditionals •Conditional execution •Alternate execution •Chained conditionals •Nested conditionals

Today's Outline

Review

Control Flow Diagrams

Basic syntax for “if”

Identifying code blocks

Demos

Page 27: [220] Conditionals · 2020. 2. 10. · Learning Objectives Today Reason about conditionals •Conditional execution •Alternate execution •Chained conditionals •Nested conditionals

Writing conditions in Python

x = input(“enter x: ”)x = int(x)

x % 2 == 0

print(“it’s even”) print(“it’s odd”)

print(“thank you”)

FalseTrue

Code:

Page 28: [220] Conditionals · 2020. 2. 10. · Learning Objectives Today Reason about conditionals •Conditional execution •Alternate execution •Chained conditionals •Nested conditionals

Writing conditions in Python

Code:

x = input(“enter x: ”)x = int(x)

x = input(“enter x: ”)x = int(x)

x % 2 == 0

print(“it’s even”) print(“it’s odd”)

print(“thank you”)

FalseTrue

Page 29: [220] Conditionals · 2020. 2. 10. · Learning Objectives Today Reason about conditionals •Conditional execution •Alternate execution •Chained conditionals •Nested conditionals

Writing conditions in Python

Code:

x = input(“enter x: ”)x = int(x)

if x % 2 == 0:

boolean expression

x = input(“enter x: ”)x = int(x)

x % 2 == 0

print(“it’s even”) print(“it’s odd”)

print(“thank you”)

FalseTrue

Page 30: [220] Conditionals · 2020. 2. 10. · Learning Objectives Today Reason about conditionals •Conditional execution •Alternate execution •Chained conditionals •Nested conditionals

Writing conditions in Python

Code:

x = input(“enter x: ”)x = int(x)

if x % 2 == 0:print(“it’s even”)

x = input(“enter x: ”)x = int(x)

x % 2 == 0

print(“it’s even”) print(“it’s odd”)

print(“thank you”)

FalseTrue

Page 31: [220] Conditionals · 2020. 2. 10. · Learning Objectives Today Reason about conditionals •Conditional execution •Alternate execution •Chained conditionals •Nested conditionals

Writing conditions in Python

Code:

x = input(“enter x: ”)x = int(x)

if x % 2 == 0:print(“it’s even”)

else:print(“it’s odd”)

x = input(“enter x: ”)x = int(x)

x % 2 == 0

print(“it’s even”) print(“it’s odd”)

print(“thank you”)

FalseTrue

colons will almost always befollowed by a tabbed new line

Page 32: [220] Conditionals · 2020. 2. 10. · Learning Objectives Today Reason about conditionals •Conditional execution •Alternate execution •Chained conditionals •Nested conditionals

Writing conditions in Python

Code:

x = input(“enter x: ”)x = int(x)

if x % 2 == 0:print(“it’s even”)

else:print(“it’s odd”)

print(“thank you”)

x = input(“enter x: ”)x = int(x)

x % 2 == 0

print(“it’s even”) print(“it’s odd”)

print(“thank you”)

FalseTrue

Page 33: [220] Conditionals · 2020. 2. 10. · Learning Objectives Today Reason about conditionals •Conditional execution •Alternate execution •Chained conditionals •Nested conditionals

Writing conditions in Python

Code:

x = input(“enter x: ”)x = int(x)

if x % 2 == 0:print(“it’s even”)

else:print(“it’s odd”)

print(“thank you”)

x = input(“enter x: ”)x = int(x)

x % 2 == 0

print(“it’s even”) print(“it’s odd”)

print(“thank you”)

FalseTrue

Page 34: [220] Conditionals · 2020. 2. 10. · Learning Objectives Today Reason about conditionals •Conditional execution •Alternate execution •Chained conditionals •Nested conditionals

Writing conditions in Python

Code:

x = input(“enter x: ”)x = int(x)

if x % 2 == 0:print(“it’s even”)

else:print(“it’s odd”)print(“good!”)

print(“thank you”)

x = input(“enter x: ”)x = int(x)

x % 2 == 0

print(“it’s even”)print(“it’s odd”)print("good!")

print(“thank you”)

FalseTrue

Page 35: [220] Conditionals · 2020. 2. 10. · Learning Objectives Today Reason about conditionals •Conditional execution •Alternate execution •Chained conditionals •Nested conditionals

Writing conditions in Python

Code:

x = input(“enter x: ”)x = int(x)

if x % 2 == 0:print(“it’s even”)print(“we wanted odd”)

else:print(“it’s odd”)print(“good!”)

print(“thank you”)

x = input(“enter x: ”)x = int(x)

x % 2 == 0

print(“it’s even”)print("we wanted odd")

print(“it’s odd”)print("good!")

print(“thank you”)

FalseTrue

Page 36: [220] Conditionals · 2020. 2. 10. · Learning Objectives Today Reason about conditionals •Conditional execution •Alternate execution •Chained conditionals •Nested conditionals

Writing conditions in Python

Code:

x = input(“enter x: ”)x = int(x)

if x % 2 == 0:print(“it’s even”)print(“we wanted odd”)

else:print(“it’s odd”)print(“good!”)

print(“thank you”)print(“all done”)

x = input(“enter x: ”)x = int(x)

x % 2 == 0

print(“it’s even”)print("we wanted odd")

print(“it’s odd”)print("good!")

print(“thank you”)print("all done")

FalseTrue

Page 37: [220] Conditionals · 2020. 2. 10. · Learning Objectives Today Reason about conditionals •Conditional execution •Alternate execution •Chained conditionals •Nested conditionals

Today's Outline

Review

Control Flow Diagrams

Basic syntax for “if”

Identifying code blocks

Demos

Page 38: [220] Conditionals · 2020. 2. 10. · Learning Objectives Today Reason about conditionals •Conditional execution •Alternate execution •Chained conditionals •Nested conditionals

Code Blocks

Code:

x = input(“enter x: ”)x = int(x)

if x % 2 == 0:print(“it’s even”)print(“we wanted odd”)

else:print(“it’s odd”)print(“good!”)

print(“thank you”)print(“all done”)

block of codeinside “if”

block of codeinside “else”

Page 39: [220] Conditionals · 2020. 2. 10. · Learning Objectives Today Reason about conditionals •Conditional execution •Alternate execution •Chained conditionals •Nested conditionals

Code Blocks

Code:

x = input(“enter x: ”)x = int(x)

if x % 2 == 0:print(“it’s even”)print(“we wanted odd”)

else:print(“it’s odd”)print(“good!”)

print(“thank you”)print(“all done”)

block of codeinside “if”

block of codeinside “else”

What if all this were inside a function?

Page 40: [220] Conditionals · 2020. 2. 10. · Learning Objectives Today Reason about conditionals •Conditional execution •Alternate execution •Chained conditionals •Nested conditionals

Code Blocks

Code:

def check_oddness():x = input(“enter x: ”)x = int(x)

if x % 2 == 0:print(“it’s even”)print(“we wanted odd”)

else:print(“it’s odd”)print(“good!”)

print(“thank you”)print(“all done”)

check_oddness()

block of codeinside “if”

block of codeinside “else”

block of code incheck_oddness

You need to get good at “seeing” code blocks in Python code.Even blocks inside blocks inside blocks…

Page 41: [220] Conditionals · 2020. 2. 10. · Learning Objectives Today Reason about conditionals •Conditional execution •Alternate execution •Chained conditionals •Nested conditionals

Identifying Code Blocks

Code:

def check_oddness():x = input(“enter x: ”)x = int(x)

if x % 2 == 0:print(“it’s even”)print(“we wanted odd”)

else:print(“it’s odd”)print(“good!”)

print(“thank you”)print(“all done”)

check_oddness()

Step 1: look for a colon atend of a line

Page 42: [220] Conditionals · 2020. 2. 10. · Learning Objectives Today Reason about conditionals •Conditional execution •Alternate execution •Chained conditionals •Nested conditionals

Identifying Code Blocks

Code:

def check_oddness():x = input(“enter x: ”)x = int(x)

if x % 2 == 0:print(“it’s even”)print(“we wanted odd”)

else:print(“it’s odd”)print(“good!”)

print(“thank you”)print(“all done”)

check_oddness()

Step 2: start drawing a lineon next code line, indented in

Page 43: [220] Conditionals · 2020. 2. 10. · Learning Objectives Today Reason about conditionals •Conditional execution •Alternate execution •Chained conditionals •Nested conditionals

Identifying Code Blocks

Code:

def check_oddness():x = input(“enter x: ”)x = int(x)

if x % 2 == 0:print(“it’s even”)print(“we wanted odd”)

else:print(“it’s odd”)print(“good!”)

print(“thank you”)print(“all done”)

check_oddness()

Step 3: continue down until you hitcode that is less indented

Page 44: [220] Conditionals · 2020. 2. 10. · Learning Objectives Today Reason about conditionals •Conditional execution •Alternate execution •Chained conditionals •Nested conditionals

Identifying Code Blocks

Code:

def check_oddness():x = input(“enter x: ”)x = int(x)

if x % 2 == 0:print(“it’s even”)print(“we wanted odd”)

else:print(“it’s odd”)print(“good!”)

print(“thank you”)print(“all done”)

check_oddness()

Step 4: box off the code

Page 45: [220] Conditionals · 2020. 2. 10. · Learning Objectives Today Reason about conditionals •Conditional execution •Alternate execution •Chained conditionals •Nested conditionals

Identifying Code Blocks

Code:

def check_oddness():x = input(“enter x: ”)x = int(x)

if x % 2 == 0:print(“it’s even”)print(“we wanted odd”)

else:print(“it’s odd”)print(“good!”)

print(“thank you”)print(“all done”)

check_oddness()

Step 4: box off the code

Page 46: [220] Conditionals · 2020. 2. 10. · Learning Objectives Today Reason about conditionals •Conditional execution •Alternate execution •Chained conditionals •Nested conditionals

Identifying Code Blocks

Code:

def check_oddness():x = input(“enter x: ”)x = int(x)

if x % 2 == 0:print(“it’s even”)print(“we wanted odd”)

else:print(“it’s odd”)print(“good!”)

print(“thank you”)print(“all done”)

check_oddness()

to find more boxes,look for the next colon

and repeat

Page 47: [220] Conditionals · 2020. 2. 10. · Learning Objectives Today Reason about conditionals •Conditional execution •Alternate execution •Chained conditionals •Nested conditionals

Identifying Code Blocks

Code:

def check_oddness():x = input(“enter x: ”)x = int(x)

if x % 2 == 0:print(“it’s even”)print(“we wanted odd”)

else:print(“it’s odd”)print(“good!”)

print(“thank you”)print(“all done”)

check_oddness()

to find more boxes,look for the next colon

and repeat

Page 48: [220] Conditionals · 2020. 2. 10. · Learning Objectives Today Reason about conditionals •Conditional execution •Alternate execution •Chained conditionals •Nested conditionals

Identifying Code Blocks

Code:

def check_oddness():x = input(“enter x: ”)x = int(x)

if x % 2 == 0:print(“it’s even”)print(“we wanted odd”)

else:print(“it’s odd”)print(“good!”)

print(“thank you”)print(“all done”)

check_oddness()

to find more boxes,look for the next colon

and repeat

Page 49: [220] Conditionals · 2020. 2. 10. · Learning Objectives Today Reason about conditionals •Conditional execution •Alternate execution •Chained conditionals •Nested conditionals

Identifying Code Blocks

Code:

def check_oddness():x = input(“enter x: ”)x = int(x)

if x % 2 == 0:print(“it’s even”)print(“we wanted odd”)

else:print(“it’s odd”)print(“good!”)

print(“thank you”)print(“all done”)

check_oddness()

to find more boxes,look for the next colon

and repeat

Page 50: [220] Conditionals · 2020. 2. 10. · Learning Objectives Today Reason about conditionals •Conditional execution •Alternate execution •Chained conditionals •Nested conditionals

Identifying Code Blocks

Code:

def check_oddness():x = input(“enter x: ”)x = int(x)

if x % 2 == 0:print(“it’s even”)print(“we wanted odd”)

else:print(“it’s odd”)print(“good!”)

print(“thank you”)print(“all done”)

check_oddness()

to find more boxes,look for the next colon

and repeat

Worksheet

Page 51: [220] Conditionals · 2020. 2. 10. · Learning Objectives Today Reason about conditionals •Conditional execution •Alternate execution •Chained conditionals •Nested conditionals

Today's Outline

Review

Control Flow Diagrams

Basic syntax for “if”

Identifying code blocks

Demos

Page 52: [220] Conditionals · 2020. 2. 10. · Learning Objectives Today Reason about conditionals •Conditional execution •Alternate execution •Chained conditionals •Nested conditionals

Demo: Absolute

compare 4 ways to compute the absolute of a number(step through in Interactive Exercises)

Page 53: [220] Conditionals · 2020. 2. 10. · Learning Objectives Today Reason about conditionals •Conditional execution •Alternate execution •Chained conditionals •Nested conditionals

Demo: Piecewise Function

...

0-2-4

2

1

1 2

-1

3x

y y = f(x)

Implement the f function in Python

3

Page 54: [220] Conditionals · 2020. 2. 10. · Learning Objectives Today Reason about conditionals •Conditional execution •Alternate execution •Chained conditionals •Nested conditionals

RED

Demo: Stoplight

YELLOW

GREEN

feet

color

what should the driver do?

Page 55: [220] Conditionals · 2020. 2. 10. · Learning Objectives Today Reason about conditionals •Conditional execution •Alternate execution •Chained conditionals •Nested conditionals

RED

Demo: Stoplight

YELLOW

GREEN

feet

color

smile

feet < 30

continue at same speed

stop

feet < 15

hit the gas

stop abruptly

Page 56: [220] Conditionals · 2020. 2. 10. · Learning Objectives Today Reason about conditionals •Conditional execution •Alternate execution •Chained conditionals •Nested conditionals

Demo: Date Printer

please enter a year: (YYYY): 18please enter a month (1-12): 2please enter a day (1-31): 3the date is: Sep 23rd of '19

e.g., 1st, 2nd, 3rd, etc

convert month num to name add 2000 when needed

Page 57: [220] Conditionals · 2020. 2. 10. · Learning Objectives Today Reason about conditionals •Conditional execution •Alternate execution •Chained conditionals •Nested conditionals

Demo: Better Battleship

Improvements• give more meaningful feedback (not "True" or "False")• check that user guessed in a reasonable range• choose random placement for two ships, not overlapping• show different symbols depending on which ship was hit• give user up to 3 guesses (or until they get a hit)

Page 58: [220] Conditionals · 2020. 2. 10. · Learning Objectives Today Reason about conditionals •Conditional execution •Alternate execution •Chained conditionals •Nested conditionals

Demo: Addition Tester

what is 3+5? 5what is 9+2? 10what is 9+5? 2

...

Your score is 6.5 of 10

1 point

0.5 points (close answer)

0 points

We can get random number by using the random module:

random.randint(1, 10)