Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Lecture 7 – Unit 1 – Chatbots Python – For loops + Robustness"— Presentation transcript:

1 Lecture 7 – Unit 1 – Chatbots Python – For loops + Robustness
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

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 some statements E 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

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 some statements E some statements F some statements G 1. To execute statements A B D G, to what Boolean value would conditions 1, 2, 3 and 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 need to evaluate? Answer: It is impossible to have statements A B C G executed together.

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

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?

7 ASCII Code (part of Unicode)

8 Reading Review What we can do with the "in" keyword for lists

9 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:

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)

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!”

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:

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)

14 Review – Terminology related to functions
We call a function by name Example of a function call: userName = input("Please, enter your name: ") The name of the function is input The expression in parentheses is called the argument of the function We say that a function takes an argument(s) 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’ We say that a function returns a result and the result is called the returned value

15 Review – type( ): another useful function
Built-in function Syntax: type(<expression>) How it works: The expression is evaluated type(…) is called with the result of the expression as its argument 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)

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 Variable Literal value -> both of type string, integer or float Variable Literal value -> both of type string, integer or float Result of conditional expression: True or False 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: 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

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.

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) ... compound condition

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

21 How to construct a logical expression (compound condition)?
SYNTAX for and & or: <operand> <logical_operator> <operand> not: not <operand> Logical operators operand and or operand Boolean expression 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

22 How to evaluate a logical expression (compound condition)?
Boolean truth table: and truth table or truth table not truth table Source: an-introduction-to-software-development-software-development-midterm-review

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


Download ppt "Lecture 7 – Unit 1 – Chatbots Python – For loops + Robustness"

Similar presentations


Ads by Google