From 837ac970a6b91035d18e91c3fb1b01bb2d0a0f15 Mon Sep 17 00:00:00 2001 From: David Lawler Date: Tue, 1 Mar 2022 14:25:02 +1000 Subject: [PATCH] fixed question recording with else - exit program --- asked_questions_log.txt | 6 ++---- main.py | 42 ++++++++++++++++++++++++++++------------- 2 files changed, 31 insertions(+), 17 deletions(-) diff --git a/asked_questions_log.txt b/asked_questions_log.txt index 0f8b88e..b2e3d15 100644 --- a/asked_questions_log.txt +++ b/asked_questions_log.txt @@ -4,7 +4,5 @@ test test2 test123 Testing exit code - - - - +boo +test \ No newline at end of file diff --git a/main.py b/main.py index 038b2a0..c8574cf 100644 --- a/main.py +++ b/main.py @@ -2,18 +2,34 @@ import json import sys print("Hello, I am a question answering bot.\n") question = str(input("Please enter a question, and press the ENTER key:\n")) -if question.isalpha: (print ("I think that you asked " + (question)) - # Open the file in append & read mode ('a+') - with open("asked_questions_log.txt", "a+") as file_object: +#if question.isalpha(): +# print ("I think that you asked " + (question)) +# # Open the file in append & read mode ('a+') +# with open("asked_questions_log.txt", "a+") as file_object: +# # Move read cursor to the start of file. +# file_object.seek(0) +# # If file is not empty then append '\n' +# data = file_object.read(100) +# if len(data) > 0 : +# file_object.write("\n") +# # Append text at the end of file +# file_object.write(question) + +if(question and question.strip()): + print ("I think that you asked " + (question)) + # Open the file in append & read mode ('a+') + with open("asked_questions_log.txt", "a+") as file_object: # Move read cursor to the start of file. - file_object.seek(0) - # If file is not empty then append '\n' - data = file_object.read(100) - if len(data) > 0 : - file_object.write("\n") + file_object.seek(0) + # If file is not empty then append '\n' + data = file_object.read(100) + if len(data) > 0 : + file_object.write("\n") # Append text at the end of file - file_object.write(question)) - + file_object.write(question) +else : + print ("Goodbye!") + sys.exit #else: # sys.exit() @@ -29,8 +45,8 @@ if question.isalpha: (print ("I think that you asked " + (question)) #jaccard_similarity(list1, list2) -with open('faq.json', 'r') as f: - questionBank = json.load(f) +#with open('faq.json', 'r') as f: +# questionBank = json.load(f) # Output json file for testing -print(questionBank) \ No newline at end of file +#print(questionBank) \ No newline at end of file