From 34eb15c61a5d26df307a00fcebe3f05daee42341 Mon Sep 17 00:00:00 2001 From: Nikolaos Schoinas Date: Thu, 26 Nov 2020 14:41:22 +0000 Subject: [PATCH] Add functionality in order to get numbers and weights from files. --- .../average-squares-example/average_squares/squares.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/week06/average-squares-example/average_squares/squares.py b/week06/average-squares-example/average_squares/squares.py index bd703cd..d781981 100644 --- a/week06/average-squares-example/average_squares/squares.py +++ b/week06/average-squares-example/average_squares/squares.py @@ -53,14 +53,16 @@ def convert_numbers(list_of_strings): if __name__ == "__main__": parser = ArgumentParser(description="Weighted mean of squared numbers") - parser.add_argument("numbers", nargs='+', help="the list of numbers") - parser.add_argument("--weights", "-w", nargs='+', help="the list of weights") + parser.add_argument("numbers", help="the lifile containing the numbers") + parser.add_argument("--weights", "-w", help="the optional file containing the weights") arguments = parser.parse_args() - numbers = convert_numbers(arguments.numbers) + with open(arguments.numbers) as file: + numbers = [int(x) for x in next(file).split()] if arguments.weights: - weights = convert_numbers(arguments.weights) + with open(arguments.weights) as file: + weights = [int(x) for x in next(file).split()] else: weights = [1] * len(numbers)