1
play

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 =


  1. xkcd.com/518 1

  2. Annoucnements Homework 1 due Wednesday 2

  3. Survey of Computer Science Decision Trees, Conditionals Kevin Walsh kwalsh@holycross.edu Readings: CSI 9.4, Py 1.6

  4. Example Program >>> lbs = 5 lbs = 5 >>> kg = lbs * 0.45 kg = lbs * 0.45 >>> kg kg 4

  5. A Subtle Distinction: Interactive vs. Script Mode Why is output different in interactive mode? Interactive Mode Non-Interactive "Script" Mode Read-evaluate-print cycle: Read-evaluate cycle Prompts user with ">>>", Read a statement from a file. 1. 1. then reads from keyboard. Evaluate the statement. 2. Evaluate the statement. 2. Displays the result , if any. 3. 5

  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. Statement Effect (when evaluated) x = 42 print x 6

  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. Expression Result (after evaluation) 5+40+2 str(35-3) "Max is " + str(5*7) 7

  8. Sequential Programming Sequential Programming: statements evaluated in order, top to bottom. # Program: winter.py answer = raw_input("Do you like to ski? ") print "You said " + answer print "Great!" 8

  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 9

  10. Decision Trees We would like to be able to respond to the user's input, evaluating different statements depending on what she enters. "Do you like to ski?" answer == "yes" 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. 10

  11. Conditional Statements # Program: skiReport.py answer = raw_input("Do you like to ski? ") if answer == "yes": print "You will love it here!" else: print "Better learn!" General form for a conditional statement: if [condition] : [statements] else: [statements] 11

  12. Conditions Conditions are expressions that evaluate to a True or False result. Examples: x < y 21 > age userName == "Frank" oranges != apples Many other operators: <=, >=, and, or, not , … 12

  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" 13

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

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

  16. Nested Conditional Example 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!" 16

  17. Multiple Choices "Guess a number:" "You win!" "Too high!" "Close" "Too low!" "Thanks for playing" 17

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

  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!" 19

  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!" 20

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

  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 22

  23. The Decision Tree 23

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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend