CMPT 120 Lecture 6 – Unit 1 – Chatbots Python – More on Conditional statements and Boolean Expressions
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
Review from last lecture What is wrong with this Python code fragment? https://repl.it/repls/OverdueSadBackup # 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!")
Review from last lecture What is wrong with this Python code fragment? Note the usefulness of the comments! https://repl.it/repls/InconsequentialMelodicConference # 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?")
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: "))
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...
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
Last Lecture, we did … 1. Fortune Cookie Generator : https://repl.it/repls/RuddyIdealApplets 2. How's it going? Chatbot - version 1 (if/else) https://repl.it/repls/BusyOnerlookedIteration 3. How's it going? Chatbot - version 2 (if/elif/else) https://repl.it/repls/DefensiveVeneratedMicrobsd 4. How's it going? Chatbot - version 3 (if/elif/elif/else) https://repl.it/repls/DopeyVitalApplicationprogrammer 5. How's it going? Chatbot - version 4 (if/elif/else and compound condition using “or”) https://repl.it/repls/MushyFaroffQuadrants
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")
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")
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
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
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
Homework Problem Statement Write a login program, which allows a user to login in with a password
Examples of Boolean
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?