Python Chatbots, For Loops, and Robustness in Homework and Reviews

cmpt 120 n.w
1 / 23
Embed
Share

Explore concepts related to Python chatbots, for loops, and program robustness through examples, exercises, and reviews. Learn about building a login program, understanding if-else conditions, string manipulation functions, and comparisons in Python programming.

  • Python Programming
  • Chatbots
  • For Loops
  • String Manipulation
  • Program Robustness

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. CMPT 120 Lecture 7 Unit 1 Chatbots Python For loops + Robustness

  2. Homework - Show Case Problem Statement Write a login program, which allows a user to login in with a password Thank you for sending your link https://repl.it/repls/ForcefulTautGoals https://repl.it/repls/SingleGorgeousUpgrades https://repl.it/repls/SuperbDownrightChapter https://repl.it/repls/VillainousSandybrownState https://repl.it/@AnsonWong1/YearlyAbleModes 2

  3. Review - Little Exercise version 1 + Answers if condition 1 : if condition 2 : some statements A if condition 3 : some statements B else : some statements C if condition 4 : some statements D else : some statements E else : some statements F some statements G 1. Which statements are executed if condition 1 is False? Answer: F and G 2. Which statements are executed if condition 1 is True and condition 2 is False? Answer: E and G 3

  4. Review - Little Exercise version 2 + Answers if condition 1 : if condition 2 : some statements A if condition 3 : some statements B else : some statements C if condition 4 : some statements D else : some statements E else : some statements F some statements G 1. To execute statements A B D G, to what Boolean value would conditions 1, 2, 3 and 4 need to evaluate? Answer: condition 1, 2, 3, and 4 would need to be True. 2. To execute statements A B C G, to what Boolean value would conditions 1, 2, 3 and 4 need to evaluate? Answer: It is impossible to have statements A B C G executed together. 4

  5. Reading Review 1. What does the string strip function (method) do? 2. What string function (method) should we use to replace all occurrences of 8 in a string with 9 ? 3. What could be wrong with this code? if reply.lower().strip(" ") == "GOOD" print("Good!") 5. What does this code output? movies = ["Superman", "Frozen", "X-Men"] print("x-men" in movies) 5

  6. Review - Examples of Boolean "great" == "Great" -> False "Great!" == "Great" -> False "Great!" == "Great!" -> True "23" < "19" -> False "100" < "19" -> True How does this work? How can we compare characters? 6

  7. ASCII Code (part of Unicode) 7

  8. Reading Review What we can do with the "in" keyword for lists https://repl.it/repls/YawningSpanishOpposites 8

  9. Lets build a simple game! Problem Statement Write a guessing game, which allows a user to guess a number between 1 and 10 My solution: https://repl.it/repls/QuaintBouncyHarddrive 9

  10. Guessing Game Testing our guessing game: Test case 1 : input != number to guess Test case 2 : input == number to guess Test case 3 : invalid input -> 53 (outside range) 10

  11. Your turn! Problem Statement Write a guessing game, which allows a user to guess a number between 1 and 10 Requirements: Let s make your guessing game more responsive How? By telling the user when she/he has entered a number that was < 1 You have entered a number < 1! or > 10 You have entered a number > 10! 11

  12. Improving our Guessing Game Problem Statement Write a guessing game, which allows a user to guess a number between 1 and 10 Requirements: Let s make your guessing game more robust So it does not crash when the player enters unexpected input like banana My Solution: https://repl.it/repls/IroncladUnhealthyDaemons 12

  13. You have 3 guesses! Wouldn t it be nice to play our guessing game many times without having to press Run over and over again? Problem Statement Write a guessing game, which allows a player to guess a number between 1 and 10 in 3 guesses! My Solution: Use a for loop (iteration) https://repl.it/repls/LeanCyanInstances 13

  14. Review Terminology related to functions 1. We call a function by name Example of a function call: userName = input("Please, enter your name: ") 2. The nameof the function is input 3. The expression in parentheses is called the argument of the function userName = input("Please, enter your name: ") 4. We say that a function takes an argument(s) 5. The result of the function is what the function produces In the above example, the result of the function is what the user has typed, i.e., her/his name, which is assigned to userName Anne 6. We say that a function returns a result and the result is called the returned value 14

  15. Review type( ): another useful function Built-in function Syntax: type(<expression>) How it works: 1. The expression is evaluated 2. type( ) is called with the result of the expression as its argument 3. type( ) returns a value which is the data type of its argument, i.e., the result of the expression Examples: type(123) type("Hello") type("123") pi = 3.14 type(pi) 15

  16. How to construct a condition? SYNTAX: <operand> <operator> <operand> Relational operators (or comparison operators) < less than > greater than <= less than or equal to >= greater than or equal to == equal to != not equal to -> both of type string, integer or float Variable Literal value Variable Literal value -> both of type string, integer or float Result of conditional expression: True or False 16 Relational operators (< , >=, ==, ) may connect expressions evaluating to different types of values

  17. How to construct a condition? SYNTAX: .<method>( ) <function>( ) operator Example: <string>.isdigit( ) <string>.isalpha( ) etc Example: in containment test operator Example: all( ) etc Result of conditional expression: True or False We have built-in functions returning numerical values: len( hello ) returns 5 int( 5 ) returns 5 We have methods returning numerical values: hello .find( lo ) returns 3 Similarly, we have built-in functions returning Boolean values: all([1<2,2<4,5==5]) returns True We have methods returning Boolean values: 123456 .isdigit() returns True 123456 .isalpha() returns False 17

  18. What if we have more than 1 conditions? In our Guessing Game If the user enters a guess (a number) between 1 and 10 -> valid data If the user enters a guess < 1 or > 10 -> invalid data Guardian code (input validation code): if userGuess < 1 or userGuess > 10 : print("Your guess should be a number between 1 and 10...") # then terminate program else : ... This is called a compound condition: a condition that Is composed of > 1 condition. 18

  19. What if we have more than 1 conditions? We could also create the following guardian code (input validation code): if userGuess >= 1 and userGuess <= 10 : # Did the user guess correctly? # Display appropriate message if number == userGuess : print("Wow! You got it!") else : print("Sorry! You missed! The number was %d." %number) else : ... compound condition 19

  20. How to construct a logical expression (compound condition)? Conditions (Boolean expressions, i.e., expressions producing Boolean values), can be connected via the logical operators and,or, not creating logical expressions Such logical expressions are called compound conditions 20

  21. How to construct a logical expression (compound condition)? SYNTAX for and & or: <operand> <logical_operator> <operand> not: not <operand> Logical operators operand operand and or Boolean expression Boolean expression operand not Result of compound conditional expression: True or False 21 Boolean operators and, or must connect two Boolean expressions Boolean operator not must be applied to one Boolean expression

  22. How to evaluate a logical expression (compound condition)? Boolean truth table: and truth table or truth table not truth table 22 Source: http://www.slideshare.net/drjimanderson/ an-introduction-to-software-development-software-development-midterm-review-45791774

  23. Next Lecture Let s see how much we have learnt so far by having a little practice exam! We ll have plenty of help while going through this activity 23

More Related Content