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/SpectacularDeeppinkVaporware
Review from last lecture Output Assigning a value to a variable Input Using a variable Concatenation https://repl.it/repls/GrossElementaryApplicationprogram
Review from last lecture Prompt A space A space Function Argument https://repl.it/repls/ShoddyHollowUnix
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) http://interactivepython.org/runestone/static/thinkcspy/SimplePythonData/VariableNamesandKeywords.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)
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!)
Don’t forget to … Update your algorithm -> comments Translate them into Python Test your program
Let’s review some concepts How do you print in Python? What is the command or function to get input from the user? 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. How do you concatenate two words (strings) in Python? How do you store an input from the user? This allows your program to remember data. What symbol do we use to assign a value to a variable?
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?
Let’s create another chatbot! Problem Statement: Write a chatbot that prints random cookie fortune statements
Let’s review some more concepts How do we make a list in Python? What module do we need to import to randomly choose something from a list? How can we test smaller pieces of our Python code? What is concatenation? What does a dot after a module name do? What is the only kind of symbol we can have in a variable name?
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...
Your turn! Problem Statement Modify the How's it Going chatbot to use 2 "elif" statements
Review: Conditional statement if <boolean expression>: ... elif <boolean expression>: else: The only thing required in a conditional Optional. Can be repeated Optional. You can only have one else per if
Let’s review some concepts What does a conditional statement do? Is the if part of a conditional statement mandatory? Is the else part of a conditional statement mandatory? What values can a Boolean expression produce? What is wrong with this code fragment? if color = "purple": print("Cool!")
Next Lecture We’ll continue practicing using conditional statements in our Python programs