CMPT 120 Lecture 7 Unit 1 Chatbots Python

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

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/Forceful. Taut. Goals https: //repl. it/repls/Single. Gorgeous. Upgrades https: //repl. it/repls/Superb. Downright. Chapter https: //repl. it/repls/Villainous. Sandybrown. State https: //repl. it/@Anson. Wong 1/Yearly. Able. Modes 2

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

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

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

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

ASCII Code (part of Unicode) 7

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

Let’s 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/Quaint. Bouncy. Harddrive 9

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

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

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/Ironclad. Unhealthy. Daemons 12

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/Lean. Cyan. Instances 13

Review – Terminology related to functions 1. We call a function by name • Example of a function call: user. Name = input("Please, enter your name: ") 2. The name of the function is input 3. The expression in parentheses is called the argument of the function user. Name = 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 user. Name ‘Anne’ 6. We say that a function returns a result and the result is called the returned value 14

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

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 not equal to • 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

How to construct a condition? SYNTAX: . <method>(…) Example: <string>. isdigit( ) <string>. isalpha( ) etc… <function>(…) operator Example: all(…) etc… Example: in containment test operator 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

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 user. Guess < 1 or user. Guess > 10 : This is called a print("Your guess should be a compound condition: a number between 1 and 10. . . ") condition that Is composed # then terminate program of > 1 condition. else : . . . 18

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

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

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

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-midterm-review-45791774

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
- Slides: 23