""" Operating Systems Python Warmup assignment - Jose Rodriguez """ import argparse class Default_dict: def __init__(self, answers, default): self.answers = answers self.default = default def __getitem__(self, key): if key not in self.answers: return self.default return self.answers[key] first_question_responses = Default_dict({ 'female': 'How excellent!', 'male': 'Me too.' }, 'Nice!') second_question_responses = Default_dict({ 'no': 'Too bad! Anyway, what\'s an animal you like and two you don\'t', 'yes!': 'Excellent, I am too. What\'s an animal you don\'t like and two you do?', '': 'Being shy is alright! What\'s an animal you don\'t like and two you do?' }, 'Nice! What\'s an animal you don\'t like and two you do?') def create_input_function(args): if args.file: try: input_file = open(args.file, 'r') def out(): line_that_was_read = input_file.readline() print(f'Read input from file: {line_that_was_read}', end='') return line_that_was_read return out except Exception: print(f'Cannot read "{args.file}"! Please try a different file!') exit(1) else: return input def main(): parser = argparse.ArgumentParser(description="Python warmup chatbot!") parser.add_argument('--file', default=None, type=str, help="if set, input from the user will be read from this file instead of from the console.") args = parser.parse_args() read_input = create_input_function(args) print('Hello, are you male or female?') answer = read_input() question = first_question_responses[answer] + ' Are you a CS major?' if not answer: print('Being shy is alright, maybe some other time!') print('Are you a CS major?') else: print(question) answer = read_input().lower() question = second_question_responses[answer] likes_first_word = answer == 'no' print(question) answer = read_input().replace(',', '').split() if len(answer) < 2: print('Haha funny. Bye for now.') return if likes_first_word: print(f'{answer[0]} awesome, but I hate {answer[-1]} too. Bye for now') else: print(f'{answer[-1]} awesome, but I hate {answer[0]} too. Bye for now') if __name__ == '__main__': main()