Skip to content

Commit

Permalink
Addresses UCL-RITS#140 to use arg parsing for squares.py
Browse files Browse the repository at this point in the history
  • Loading branch information
twemyss committed Nov 26, 2020
1 parent 2054abb commit d14b794
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions week06/average-squares-example/average_squares/squares.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Computation of weighted average of squares."""

from argparse import ArgumentParser

def average_of_squares(list_of_numbers, list_of_weights=None):
""" Return the weighted average of a list of values.
Expand Down Expand Up @@ -51,11 +52,15 @@ def convert_numbers(list_of_strings):


if __name__ == "__main__":
numbers_strings = ["1","2","4"]
weight_strings = ["1","1","1"]
parser = ArgumentParser(description="Weighted square average of a list of values")

parser.add_argument('numbers', nargs='+', default=["1","2","4"])
parser.add_argument('--weights', '-w', nargs='+', default=["1","1","1"])

arguments = parser.parse_args()

numbers = convert_numbers(numbers_strings)
weights = convert_numbers(weight_strings)
numbers = convert_numbers(arguments.numbers)
weights = convert_numbers(arguments.weights)

result = average_of_squares(numbers, weights)

Expand Down

0 comments on commit d14b794

Please sign in to comment.