Skip to content

Commit

Permalink
Solutions for argparse exercise Addresses UCL-RITS#140
Browse files Browse the repository at this point in the history
  • Loading branch information
esl-lewis committed Nov 26, 2020
1 parent 9ed24ad commit dd1917a
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 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,12 +52,19 @@ def convert_numbers(list_of_strings):


if __name__ == "__main__":
numbers_strings = ["1","2","4"]
weight_strings = ["1","1","1"]
parser = ArgumentParser(description="!")
parser.add_argument('numberlist', type=int, nargs='+', help="enter the list of numbers")
parser.add_argument('--weights', type=int, nargs='+', help="enter the list of weights")
arguments= parser.parse_args()

numbers_strings = arguments.numberlist
if arguments.weights:
weight_strings = arguments.weights
else:
weights = [1] * len(numbers_strings)

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

result = average_of_squares(numbers, weights)
result = average_of_squares(numbers_strings, weight_strings)

print(result)
print(result)

0 comments on commit dd1917a

Please sign in to comment.