1 Annoucnements Homework 1 due Wednesday 2 Survey of Computer - - PowerPoint PPT Presentation

1
SMART_READER_LITE
LIVE PREVIEW

1 Annoucnements Homework 1 due Wednesday 2 Survey of Computer - - PowerPoint PPT Presentation

xkcd.com/518 1 Annoucnements Homework 1 due Wednesday 2 Survey of Computer Science Decision Trees, Conditionals Kevin Walsh kwalsh@holycross.edu Readings: CSI 9.4, Py 1.6 Example Program >>> lbs = 5 lbs = 5 >>> kg =


slide-1
SLIDE 1

1

xkcd.com/518

slide-2
SLIDE 2

2

Annoucnements

Homework 1 due Wednesday

slide-3
SLIDE 3

Kevin Walsh kwalsh@holycross.edu

Survey of Computer Science

Decision Trees, Conditionals

Readings: CSI 9.4, Py 1.6

slide-4
SLIDE 4

4

Example Program

lbs = 5 kg = lbs * 0.45 kg >>> lbs = 5 >>> kg = lbs * 0.45 >>> kg

slide-5
SLIDE 5

5

A Subtle Distinction: Interactive vs. Script Mode

Interactive Mode Read-evaluate-print cycle:

1.

Prompts user with ">>>", then reads from keyboard.

2.

Evaluate the statement.

3.

Displays the result, if any. Non-Interactive "Script" Mode Read-evaluate cycle

1.

Read a statement from a file.

2.

Evaluate the statement.

Why is output different in interactive mode?

slide-6
SLIDE 6

6

Statements vs. Expressions

Statements

  • Do something when they are evaluated.
  • E.g.: an assignment statement or a print statement
  • Do not produce any value.

Effect (when evaluated) Statement

x = 42 print x

slide-7
SLIDE 7

7

Statements vs. Expressions

Expressions

  • Are evaluated to produce some result value.
  • E.g.: things made with +, -, *, etc.
  • E.g.: things on right side of an assignment statement.

Result (after evaluation) Expression

5+40+2 str(35-3) "Max is " + str(5*7)

slide-8
SLIDE 8

8

Sequential Programming

Sequential Programming: statements evaluated in

  • rder, top to bottom.

# Program: winter.py answer = raw_input("Do you like to ski? ") print "You said " + answer print "Great!"

slide-9
SLIDE 9

9

Colossal Cave Adventure

You are in a low N/S passage at a hole in the floor. The hole goes down to an E/W passage. There are bars of silver here! > take silver OK > go south A little dwarf just walked around a corner, saw you, threw a little axe at you (which missed), cursed, and ran away. You're in Hall of Mt King. There is a little axe here. A cheerful little bird is sitting here singing. > take axe

slide-10
SLIDE 10

10

Decision Trees

We would like to be able to respond to the user's input, evaluating different statements depending

  • n what she enters.

"Do you like to ski?" print "You will love it here!" print "You should learn!"

A decision tree diagrams the paths a program can take depending on outcome of various tests.

answer == "yes"

slide-11
SLIDE 11

11

# Program: skiReport.py answer = raw_input("Do you like to ski? ") if answer == "yes": print "You will love it here!" else: print "Better learn!"

Conditional Statements

if [condition]: [statements] else: [statements] General form for a conditional statement:

slide-12
SLIDE 12

12

Conditions

Conditions are expressions that evaluate to a True or False result. Examples: x < y 21 > age userName == "Frank"

  • ranges != apples

Many other operators: <=, >=, and, or, not, …

slide-13
SLIDE 13

13

Compound Statements

A series of statements (with same indentation) forms a block or compound statement.

answer = raw_input("Do you like to ski? ") if answer == "yes": print "You will love it here!" print "We have lots of snow!" else: print "Better learn!" print "You will enjoy winter more!" print "Goodbye"

slide-14
SLIDE 14

14

Control Flow

"Do you like to ski?" "You will love it here!" "You should learn!" answer == "yes" "We have lots of snow!" "You will enjoy winter more!" "Goodbye!"

slide-15
SLIDE 15

15

Multiple Decisions

Can use a nested conditional statements to program this decision tree. "Do you like to ski?" "Downhill or cross country? "You should learn!" ans == "yes" "Try Wachusett " "Try the local trails." ans == "downhill"

slide-16
SLIDE 16

16

ans = raw_input("Do you like to ski? ") if ans == "yes": ans = raw_input("Downhill or cross country? ") if ans == "downhill": print "Try Wachusett!" else: print "Try the local trails." else: print "You should learn!"

Nested Conditional Example

slide-17
SLIDE 17

17

Multiple Choices

"Guess a number:" "Close" "You win!" "Too high!" "Too low!" "Thanks for playing"

slide-18
SLIDE 18

18

Multiple Choices

"Guess a number:" "Close!" "You win!" G == S "Too high" "Too low" "Thanks for playing" S-5<G<S+5 G < S True False True False True False

slide-19
SLIDE 19

19

Using if .. elif .. else

secret = 42 guess = input("Guess a number: ") if guess == secret: print "You win!" else: if (secret-5 < guess < secret+5): print "Close!" else: if guess < secret: print "Too low" else: print "Too high" print "Thanks for playing!"

slide-20
SLIDE 20

20

Using if .. elif .. else

secret = 42 guess = input("Guess a number: ") if guess == secret: print "You win!" elif (secret-5 < guess < secret+5): print "Close!" elif guess < secret: print "Too low" else: print "Too high" print "Thanks for playing!"

slide-21
SLIDE 21

21

Using if .. elif .. else

if [condition]: [statements] elif [condition]: [statements] elif [condition]: [statements] ... else: [statements]

slide-22
SLIDE 22

22

Practice Problem

Consider a program that does the following:

1) Ask for the user's age 2) If the user is over 65, it says "You get the senior discount". 3) If the user is under 12, it says "You get the child discount". 4) Otherwise, it asks if the user is a student

a) If the user answers 'yes', it says "You get the student discount". b) Otherwise it says "You pay the regular price".

  • Draw the decision tree for the above program
  • Write the Python code for the above program
slide-23
SLIDE 23

23

The Decision Tree

slide-24
SLIDE 24

24

The Python Code

# Program: discounts.py age = input("How old are you? ") if age > 65: print "You get the senior discount!" elif age < 12: print "You get the child discount!" else: student = raw_input("Are you a student? ") if student == "yes": print "You get the student discount!" else: print "You pay the regular price."