Download presentation
Presentation is loading. Please wait.
1
CMPT 120 Lecture 6 – Unit 1 – Chatbots
Python – More on Conditional statements and Boolean Expressions
2
Homework Show Case Problem Statement
Write a chatbot that prints random cookie fortune statements # Fortune Cookie # Gives a random fortune # Date: 5/15/19 import random # First gives a prompt to break the cookie. cookie = input("Hello there, press enter to break this cookie.") # This is the list of possible fortune given by the cookie. fortune = ["Gift of Luck", "Gift of Love", "Gift of Power", "Gift of Misfortune", "Gift of Friendship"] # this random chooses from the list of fortunes luck = random.choice(fortune) # prints the outcome print(luck) Thank you for sharing your link
3
Review from last lecture
What is wrong with this Python code fragment? # Say How is it going? and get user's reply reply = input("How is it going? ") # Say Glad to hear if the user is doing well if reply == "Awesome!" : print("Glad to hear!") else: # And Sorry things are not going well! otherwise print("Sorry to hear things are not going well!")
4
Review from last lecture
What is wrong with this Python code fragment? Note the usefulness of the comments! # Chatbot asks How old are you? age = input("How old are you? ") # If the user is old enough to drink ... if age >= 19 : # Chatbot suggest an alcoholic drink. print("How about a beer?") else : # Otherwise, a non-alcoholic drink. print("How about a Shirley Temple?")
5
Conversion functions int( ... ) -> convert into an integer
float( ... ) -> convert into a floating point number str( ... ) -> convert into a string Example gradeMT = float(input("Please, enter MT grade: "))
6
Review Input function input( ) or input( prompt ) How it works:
The prompt is printed on the screen The user enters the requested data then presses Enter This data is returned from the function input( ) into our program as a string GPS: When prompting the user, we must give her/him clear and unambiguous instructions: Description of what to enter: Enter your name: Example of values to enter: (yes/no) The format of this value (if possible) The range of the value: Enter a number between 1 and 10: etc...
7
GPS related to input and output statements and user interaction
From a user perspective, i.e., from the perspective of the person typing her/his name, which user interaction would we prefer? User interaction #1 or User interaction #1 Why? User interaction # 1 User interaction # 2
8
Last Lecture, we did … 1. Fortune Cookie Generator :
2. How's it going? Chatbot - version 1 (if/else) 3. How's it going? Chatbot - version 2 (if/elif/else) 4. How's it going? Chatbot - version 3 (if/elif/elif/else) 5. How's it going? Chatbot - version 4 (if/elif/else and compound condition using “or”)
9
Review from last lecture
Do these 2 Python code fragments produce the same result? A B grade = 78 if grade < 60 : print("F") else : if grade < 70 : print("D") if grade < 80 : print("C") if grade < 90 : print("B") print("A") grade = 78 if grade < 60 : print("F") elif grade < 70 : print("D") elif grade < 80 : print("C") elif grade < 90 : print("B") else : print("A")
10
Review from last lecture
Do these 2 Python code fragments produce the same result? B C grade = 78 if grade < 60 : print("F") elif grade < 70 : print("D") elif grade < 80 : print("C") elif grade < 90 : print("B") else : print("A") grade = 78 if grade < 60 : print("F") if grade < 70 : print("D") if grade < 80 : print("C") if grade < 90 : print("B") else : print("A")
11
Hand Tracing What is it? Why doing it?
When a software developer manually goes through her/his code (program) and “execute” it as if s/he was a computer, mimicking the Python Interpreter Why doing it? To figure out what our program does/produces, hence to verify whether our program is solving the problem To determine whether our program contains any errors
12
Little Exercise – Version 1
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
13
Little Exercise – Version 2
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
14
Homework Problem Statement
Write a login program, which allows a user to login in with a password
15
Examples of Boolean
16
Next Lecture Let’s build a simple game!
Problem Statement Write a guessing game, which allows a user to guess a number between 1 and 10 Wouldn’t it be nice to play our guessing game many times without having to press Run over and over again?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.