CMPT 120 Lecture 4 Unit 1 Chatbots Python
CMPT 120 Lecture 4 – Unit 1 – Chatbots Python - Conditional statements (Branching)
Review from last lecture Name of our program Algorithm steps https: //repl. it/repls/Spectacular. Deeppink. Vaporware 2
Review from last lecture Output Assigning a value to a variable Input Using a variable Concatenation https: //repl. it/repls/Gross. Elementary. Applicationprogram 3
Review from last lecture Prompt A space Function Argument https: //repl. it/repls/Shoddy. Hollow. Unix 4
About variables There are some constraints to how you can name your variables, e. g. ● Can contain letters, numbers, and underscores ● Should start with a letter (lowercase, by convention) ● ● They can't contain spaces or symbols They can't be one of the reserved keywords (see below for list) 5 http: //interactivepython. org/runestone/static/thinkcspy/Simple. Python. Data/Variable. Namesand. Keywords. html
GPS -> Good Programming Style • Give your variables descriptive name • Why? We shall record everything we say in class about Good Programming Style on a web page accessible from the menu of our course web site Check this web page out by clicking on the menu option Good Programming Style (GPS) 6
Your turn! • Problem Statement: • Write a chatbot that can say hi and learn your name and … • After the greetings, make the chatbot ask what your favourite music band is, and let you respond • Then, the chatbot makes a comment about your response (but it cannot simply repeat what you have just said!) 7
Don’t forget to … • Update your algorithm -> comments • Translate them into Python • Test your program 8
Let’s review some concepts 1. How do you print in Python? 2. What is the command or function to get input from the user? 3. How do we call the string that is used as an argument to the function input()? An argument is what is put inside the () of a function. 4. How do you concatenate two words (strings) in Python? 5. How do you store an input from the user? This allows your program to remember data. 6. What symbol do we use to assign a value to a variable? 9
Let’s introduce Python Lists and Modules • Problem Statement: • Write a chatbot that can say hi and learn your name and … • After the greetings, make the chatbot ask what your favourite music band is, and let you respond • Then, the chatbot makes a comment about your response • This comment should not be too obviously repetitive Hum… what do you mean? 10
Let’s create another chatbot! • Problem Statement: • Write a chatbot that prints random cookie fortune statements 11
Let’s review some more concepts 1. How do we make a list in Python? 2. What module do we need to import to randomly choose something from a list? 3. How can we test smaller pieces of our Python code? 4. What is concatenation? 5. What does a dot after a module name do? 6. What is the only kind of symbol we can have in a variable name? 12
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. . . 13
Your turn! • Problem Statement • Modify the How's it Going chatbot to use 2 "elif" statements 14
Review: Conditional statement if <boolean expression>: . . . else: . . . The only thing required in a conditional Optional. Can be repeated Optional. You can only have one else per if 15
Let’s review some concepts 1. What does a conditional statement do? 2. Is the if part of a conditional statement mandatory? 3. Is the else part of a conditional statement mandatory? 4. What values can a Boolean expression produce? 5. What is wrong with this code fragment? if color = "purple": print("Cool!") 16
Next Lecture • We’ll continue practicing using conditional statements in our Python programs 17
- Slides: 17