CMPT 120 Lecture 5 Unit 1 Chatbots Python
CMPT 120 Lecture 5 – Unit 1 – Chatbots Python – More on Conditional statements
Review from last lecture 1. What is wrong with this code? 3_colours = ["blue", "green", "white", "pink"] 2. How could this code be improved? fav. Band = input() print("Oh, I like this band!") 3. Why do we want to put an import statement at the beginning (top) of a program? -> see next two slides 4. When a program executes, what is first executed? 2
Import at the top of program? Functions (like choice()) from the import module can be used in this section of my program 3
Import further down? Functions (like choice()) from the import module can not be used in this section of my program Notice that the yellow section is smaller here than on the previous slide meaning that the section of my program in which functions (like choice()) from the import module can be used is reduced. Why not maximize this section? To do so, import at the top of your program! 4
Review from last lecture • Execution Flow • Execution flow is the path the Python Interpreter takes when it executes our program • When we execute our program, the Python Interpreter starts interpreting each statement (line) of our program from its very top down to its last line, proceeding in a sequential fashion (one line at a time, top to bottom) 5
Last Lecture, we did … 6 • https: //repl. it/repls/Azure. Fancy. Menu
About commenting out code https: //repl. it/repls/Appropriate. Miserly. Lava You can comment out the code that works so you focus on testing the new code. Don’t forget to remove the # sign once all your code has been tested! Tip: CTRL+/ 7
Homework – from last lecture • Problem Statement: • Write a chatbot that prints random cookie fortune statements 8
Let’s introduce Branching (Python Conditional statements) • Problem Statement • Make a chatbot that asks a user how their day is going, and make a comment that changes depending on how the user answered How’ s it g oing? ! d o Go Not great. . . 9
Your turn! • Problem Statement • Modify the How's it Going chatbot to use 2 "elif" statements 10
Boolean Expressions -> Homework for next lecture • Problem Statement • Write a login program, which allows a user to login in with a password 11
Review: Conditional statements if statement Syntax #1: This small empty space made of a few white space (or blank) characters is called an indentation and its presence is very important in a conditional statement in Python. Boolean expression 12
Review: Conditional statements if else statement Syntax #2: Indentation Boolean expression 13
Review: Conditional statements nested if if statement 14
Review: Conditional statements chained if statement 15
Review: How nested if works • Conditional Statement – nested if statement if weekday == 0: print("That's Monday!") else: if weekday == 1: print("That's Tuesday!") else: if weekday == 2: print("That's Saturday!") else: if weekday == 3: print("That's Sunday!") else: print("Your number was not between 0 and 3 : ( !") “Green” if else is nested in the “blue” if else “Red” if else is nested in the “green” if else “Black” if else is nested in the “red” if else 16
Review: How chained if works • Conditional Statement – chained if statement if weekday == 0: print("That's Monday!") elif weekday == 1: print("That's Tuesday!") elif weekday == 2: print("That's Saturday!") elif weekday == 3: print("That's Sunday!") else: print("Your number was not between 0 and 3 : ( !") “Green” if elif is chained to the “blue” if elif “Red” if elif is chained to the “green” if elif “Black” if else is chained to the “red” if elif 17
Next Lecture • We’ll continue practicing using conditional statements in our Python programs • We shall also play around Boolean values and Boolean expressions 18
- Slides: 18