see next two slides 4. When a program executes, what is first executed?"> see next two slides 4. When a program executes, what is first executed?">
Download presentation
Presentation is loading. Please wait.
Published by현 전 Modified over 5 years ago
1
Lecture 5 – Unit 1 – Chatbots Python – More on Conditional statements
CMPT 120 Lecture 5 – Unit 1 – Chatbots Python – More on Conditional statements
2
Review from last lecture
1. What is wrong with this code? 3_colours = ["blue", "green", "white", "pink"] 2. How could this code be improved? favBand = 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?
3
Import at the top of program?
Functions (like choice()) from the import module can be used in this section of my program
4
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!
5
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)
6
Last Lecture, we did …
7
About commenting out code
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+/
8
Homework – from last lecture
Problem Statement: Write a chatbot that prints random cookie fortune statements
9
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 Also think about the various branches in web sites. If you say YES, then continue with your purchase. If you click CANCEL, you’re back to a different path. Good! How’s it going? Not great...
10
Your turn! Problem Statement
Modify the How's it Going chatbot to use 2 "elif" statements
11
Boolean Expressions -> Homework for next lecture
Problem Statement Write a login program, which allows a user to login in with a password
12
Review: Conditional statements
if statement Syntax #1: Boolean expression 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.
13
Review: Conditional statements
if else statement Syntax #2: Indentation Boolean expression
14
Review: Conditional statements
nested if statement nested if statement
15
Review: Conditional statements
chained if statement
16
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!") if weekday == 2: print("That's Saturday!") if weekday == 3: print("That's Sunday!") 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
17
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
18
Next Lecture We’ll continue practicing using conditional statements in our Python programs We shall also play around Boolean values and Boolean expressions
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.