From 598db36d0a57edd636ecc08a5b7cb82d9c1a914b Mon Sep 17 00:00:00 2001 From: Zara Alexander-Passe Date: Mon, 17 Nov 2025 09:52:26 +0000 Subject: [PATCH 1/3] Add argparse for number input --- squares.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/squares.py b/squares.py index d7e454e..f8b7bb7 100644 --- a/squares.py +++ b/squares.py @@ -1,6 +1,8 @@ """Computation of weighted average of squares.""" +import argparse + def average_of_squares(list_of_numbers, list_of_weights=None): """ Return the weighted average of a list of values. @@ -51,10 +53,14 @@ def convert_numbers(list_of_strings): if __name__ == "__main__": + parser = argparse.ArgumentParser(description="Inputs for weighted average of squares") + parser.add_argument("--numbers", nargs="+", type=int, help="List of numbers") + arguments = parser.parse_args() + numbers_strings = ["1","2","4"] weight_strings = ["1","1","1"] - numbers = convert_numbers(numbers_strings) + numbers = arguments.numbers weights = convert_numbers(weight_strings) result = average_of_squares(numbers, weights) From 953a522203eee15d9a994f71d97af78a99baa769 Mon Sep 17 00:00:00 2001 From: Zara Alexander-Passe Date: Mon, 17 Nov 2025 10:14:31 +0000 Subject: [PATCH 2/3] allow weights --- squares.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/squares.py b/squares.py index f8b7bb7..3bf00b7 100644 --- a/squares.py +++ b/squares.py @@ -55,13 +55,17 @@ def convert_numbers(list_of_strings): if __name__ == "__main__": parser = argparse.ArgumentParser(description="Inputs for weighted average of squares") parser.add_argument("--numbers", nargs="+", type=int, help="List of numbers") + parser.add_argument("--weights", nargs="+", type=float, help="List of weights") arguments = parser.parse_args() - numbers_strings = ["1","2","4"] - weight_strings = ["1","1","1"] + # numbers_strings = ["1","2","4"] + # weight_strings = ["1","1","1"] + + print(arguments.numbers) + print(arguments.weights) numbers = arguments.numbers - weights = convert_numbers(weight_strings) + weights = arguments.weights result = average_of_squares(numbers, weights) From e1e238b10b2fa9def807ef1b17f761c4f259a993 Mon Sep 17 00:00:00 2001 From: Zara Alexander-Passe Date: Mon, 17 Nov 2025 10:46:38 +0000 Subject: [PATCH 3/3] Add inputs from text files --- squares.py | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/squares.py b/squares.py index 3bf00b7..1620fd4 100644 --- a/squares.py +++ b/squares.py @@ -2,6 +2,9 @@ import argparse +import os + + def average_of_squares(list_of_numbers, list_of_weights=None): """ Return the weighted average of a list of values. @@ -54,19 +57,24 @@ def convert_numbers(list_of_strings): if __name__ == "__main__": parser = argparse.ArgumentParser(description="Inputs for weighted average of squares") - parser.add_argument("--numbers", nargs="+", type=int, help="List of numbers") - parser.add_argument("--weights", nargs="+", type=float, help="List of weights") + parser.add_argument("numbers_file", help="File containing numbers") + parser.add_argument("--weights", dest="weights_file", help="File containing weights", default=None) arguments = parser.parse_args() - - # numbers_strings = ["1","2","4"] - # weight_strings = ["1","1","1"] - print(arguments.numbers) - print(arguments.weights) + number = [] + with open(arguments.numbers_file, "r") as numbers_file: + for line in numbers_file: + number.append(float(line.strip())) + + if arguments.weights_file is not None: + weights = [] + with open(arguments.weights_file, "r") as weights_file: + for line in weights_file: + weights.append(float(line.strip())) - numbers = arguments.numbers - weights = arguments.weights + else: + weights = None - result = average_of_squares(numbers, weights) + result = average_of_squares(number, weights if weights else None) print(result) \ No newline at end of file