1 / 16

CSE 231 Lab 2

Learn how to make decisions in Python using if, elif, and else statements, and how to repeat code using while and for loops. Understand the concepts of indentation and statement suites.

ckathleen
Télécharger la présentation

CSE 231 Lab 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE 231 Lab 2

  2. Topics to cover • Decisions: Decision is how programs make choices • if , elif, and else • Repetition: Repeat until a condition is met • while loop • for loop (optional) condition ends all headers key condition : Keyword Statement “suite” of statements Indentation Statement

  3. Decisions with if grade = 3.5 if grade >= 1.0: print(“Passed”) #What gets printed? Passed

  4. Decisions with if grade = 0.5 if grade >= 1.0: print(“Passed”) #What gets printed? #nothing

  5. Decisions with if/else grade = 3.5 if grade >= 1.0: print(“Passed :) ”) else: print(“Not Passed :’( ”) #What gets printed? Passed :)

  6. Decisions with if/else grade = 0.5 if grade >= 1.0: print(“Passed :) ”) else: print(“Not Passed :’( ”) #What gets printed? Not Passed :’(

  7. Decisions with if/elif/else grade = 0.5 if grade >= 3.0: print(“Excellent”) elif grade >= 2.0: print(“Good”) else: print(“Not so good”) #What gets printed? Not so good

  8. Decisions with if/elif/else #What gets printed? grade = 3.5 if grade >= 3.0: print(“Excellent”) elif grade >= 2.0: print(“Good”) else: print(“Not so good”) Excellent ...but why not “Good” as well, since 3.5 >= 2.0?

  9. Decisions with if/elif/else grade = 2.5 if grade >= 3.0: print(“Excellent”) elif grade >= 2.0: print(“Good”) else: print(“Not so good”) #What gets printed? Good

  10. Repetition with while answer = input(“Please say Hi:”) while answer != “Hi”: print(“You do not want to say Hi ?”) answer = input(“Let’s try again… Say Hi:”) print(“Cool, let’s continue then.”) • >Please say Hi: no • >You do not want to say Hi ? • >Let’s try again… Say Hi: no • >You do not want to say Hi ? • >Let’s try again… Say Hi: Hi • >Cool, let’s continue then.

  11. Repetition with for n = 3 for i in range(n): print(i) #What gets printed? 0 1 2 why not 3?

  12. Repetition with for for i in range(2,5): print(i) #What gets printed? 2 3 4 why start at 2?

  13. Repetition with for for i in range(1,9,2): print(i) #What gets printed? 1 3 5 7 why only odds?

  14. Should I use while or for for i in range(5): print(“hello”) for: use this when you know how many times you want to iterate something e.g., to print “hello” 5 times while: when you want to continue looping until some condition is met when we do not know how many times we need to loop e.g.,continue program until user inputs “Hi” answer = input() while answer != “Hi”: answer = input()

  15. Break vs Continue statement #What gets printed? for letter in 'Python': if letter == 'h': break print('Current Letter :', letter) Current Letter : P Current Letter : y Current Letter : t for letter in 'Python': if letter == 'h': continue print('Current Letter :', letter) Current Letter : P Current Letter : y Current Letter : t Current Letter : o Current Letter : n

  16. Break vs continue The break statement in Python terminates the current loop and resumes execution at the next statement, The continue statement in Python returns the control to the beginning of the while loop.

More Related