From a22a4eed51918beae96cb32185b1d231333b3b98 Mon Sep 17 00:00:00 2001 From: iynix Date: Fri, 2 Dec 2022 18:21:27 +0000 Subject: [PATCH] make weights optional Addresses UCL-COMP0233-22-23/RSE-Classwork#41 --- squares.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/squares.py b/squares.py index cf79392..aa974e3 100644 --- a/squares.py +++ b/squares.py @@ -42,17 +42,20 @@ def convert_numbers(list_of_strings): """ all_numbers = [] - for s in list_of_strings: + if list_of_strings is None: + all_numbers = None + else: + for s in list_of_strings: # Take each string in the list, split it into substrings separated by # whitespace, and collect them into a single list... - all_numbers.extend([int(token.strip()) for token in s.split()]) + all_numbers.extend([int(token.strip()) for token in s.split()]) # ...then convert each substring into a number return all_numbers def parse_args(): parse = argparse.ArgumentParser(description='calculate square') - parse.add_argument('numbers',type=str, nargs='+',help='a list of number strings') - parse.add_argument('weights',type=str, nargs='+',help='a list of weight strings') + parse.add_argument('numbers',type=str, nargs='*',help='a list of number strings') + parse.add_argument('-w','--weights',type=str, nargs='?',default=None,action = 'store',help='a list of weight strings') args = parse.parse_args() return args @@ -60,7 +63,7 @@ def parse_args(): if __name__ == "__main__": args = parse_args() - # weight_strings = ["1","1","1"] + # weight_strings = ["1"] # input as "1 2 4" "1 1 1" numbers = convert_numbers(args.numbers) print(numbers)