From 19e4096a9a8ce8493eaa042e391596992ad54ffc Mon Sep 17 00:00:00 2001 From: David Scobie Date: Thu, 26 Nov 2020 12:04:00 +0000 Subject: [PATCH] using it with arguments in command line https://github.com/UCL-RITS/rse-classwork-2020/issues/140 @kyrlazari @wjl13910 @matlee99 --- .../average_squares/squares.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/week06/average-squares-example/average_squares/squares.py b/week06/average-squares-example/average_squares/squares.py index f0273d4..91f8df2 100644 --- a/week06/average-squares-example/average_squares/squares.py +++ b/week06/average-squares-example/average_squares/squares.py @@ -1,5 +1,7 @@ """Computation of weighted average of squares.""" import math +from argparse import ArgumentParser +import sys def average_of_squares(list_of_numbers, list_of_weights=None): """ Return the weighted average of a list of values. @@ -51,12 +53,18 @@ def convert_numbers(list_of_strings): if __name__ == "__main__": - numbers_strings = ["2","4"] + #numbers_strings = ["2","4"] + + + numbers_strings = str(sys.argv) + floats = [x for x in sys.argv[1:]] + weight_strings = ["1","0.5"] - numbers = convert_numbers(numbers_strings) + numbers = convert_numbers(floats) weights = convert_numbers(weight_strings) result = average_of_squares(numbers, weights) + print(result) \ No newline at end of file