From 22d77bd44d1a7e5f76f6145e4ede8ff383ba721e Mon Sep 17 00:00:00 2001 From: Akintola Jacob Date: Wed, 21 Aug 2024 16:32:09 +0100 Subject: [PATCH 1/2] =?UTF-8?q?fix=20some=20errors=20in=20the=20=E2=80=98W?= =?UTF-8?q?ids=5FChatbot=5FScripts.py=E2=80=99=20file=20and=20add=20more?= =?UTF-8?q?=20functions=20to=20the=20chatbot?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Beginners/PhoneBook.py | 2 +- WIDS_Lagos2020/Wids_Chatbot_Script.py | 47 +++++++++++++++++++-------- 2 files changed, 35 insertions(+), 14 deletions(-) diff --git a/Beginners/PhoneBook.py b/Beginners/PhoneBook.py index cad5fb2..d6b9102 100644 --- a/Beginners/PhoneBook.py +++ b/Beginners/PhoneBook.py @@ -73,7 +73,7 @@ def phonebook(): #Initiate a name variable name = input('Enter the name of the contact you wish to delete: ') #check if contact exists - if name in contact: + if name in contact: #print the required contact print('The contact is',name,':',contact[name]) diff --git a/WIDS_Lagos2020/Wids_Chatbot_Script.py b/WIDS_Lagos2020/Wids_Chatbot_Script.py index 38b1464..889778d 100644 --- a/WIDS_Lagos2020/Wids_Chatbot_Script.py +++ b/WIDS_Lagos2020/Wids_Chatbot_Script.py @@ -3,28 +3,49 @@ import random #Create and store greetings and likely responses in a list data structure -greetings = ["hola!I'm Annie", "hello! I'm Annie", 'hi!', 'Hi', 'hey!','hey',"What's up", 'Good day'] -responses = ['Okay',"I'm fine","I'm doing great", "I'm ok","I'm good",'Good','Awesome', 'Great','Superb','nice','ok'] +greetings = ["hola!I'm Annie,", "hello! I'm Annie,", 'hi!', 'Hi,', 'hey!','hey,',"What's up,", 'Good day,'] +# define a dictionary of responses +responses = { + "good": "I'm so glad to hear that!", + "great": "That's awesome!", + "bad": "Oh dear, sorry to hear that.", + "terrible": "That sounds tough.", + "okay": "It's okay, things will get better!", + "fine": "That's good to hear!" + } + +# define a dictionary of follow up questions +follow_up_questions = { + "good": "What's making you feel good today?", + "bad": "What's wrong?", + "terrible": "That sounds really tough. What's going on?", + "okay": "What's making you feel okay?", + "fine": "What's going on that's making you feel fine?" +} #question = ['How are you?','How are you doing?'] # Initiate a Welcome message and an introduction -name = input('Welcome! What is your name?: ') +name = input('Welcome!\nWhat is your name?: ') print('>>>') -print((random.choice(greetings),name,'!Nice to meet you')) +# pick a random greeting from the greetings list +print(random.choice(greetings) + ' ' + name + '. ' + 'Nice to meet you!') #1st conversation -feelings = input("How are you today?: ") +feelings = input("In one word, how are you today?: ").lower() -if feelings.capitalize() in responses: - print("I'm so Glad to hear that!") +# Gives a response to the user's feelings +if feelings in responses: + print(responses[feelings]) + follow_up = follow_up_questions[feelings] + if follow_up: + print(follow_up) + reason = input('> ') + print('I understand. It is absolutely ok to feel that way sometimes. Thanks for sharing.') else: - print('Oh dear!') - print('Why do you feel', feelings) - reason = input('Please tell me: ') - print('I understand.It is absolutely ok to feel that way sometimes.Thanks for sharing') - - + print("I'm not sure I understand. Can you tell me more?") + reason = input("> ") + print("I understand. It is absolutely ok to feel that way sometimes. Thanks for sharing!") #2nd Conversation #Ask for user's age From 0626f183166cc5e560a9f83b895efa8811b83e58 Mon Sep 17 00:00:00 2001 From: Akintola Jacob Date: Wed, 21 Aug 2024 16:40:15 +0100 Subject: [PATCH 2/2] Refactor greeting messages using string concatenation --- Beginners/Stringconcatenation.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Beginners/Stringconcatenation.py b/Beginners/Stringconcatenation.py index dc62a50..2b65ee1 100644 --- a/Beginners/Stringconcatenation.py +++ b/Beginners/Stringconcatenation.py @@ -1,7 +1,7 @@ #String concatenation #Using the Addition Operator: -print('Hi!' + 'I am Diyyah') +print('Hi!' + ' ' + 'I am Diyyah') #Using variables and the addition operator h = 'Hello' @@ -11,9 +11,9 @@ #Second Example name = input('What is your name?: ') message = 'Nice to meet you' -print('Hi!' + name + '.' + message) +print('Hi!' + ' ' + name + '.' + ' ' + message) #Using the Multiplication operator -print(message * 3) +print(f'{message}\n' * 3)