Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit de3e87e

Browse files
committed
ch03: valid solution
1 parent 36572e3 commit de3e87e

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

03_picnic/picnic.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def get_args():
1717
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
1818
)
1919

20-
parser.add_argument("positional", metavar="str", help="Item(s) to bring")
20+
parser.add_argument("items", metavar="str", help="Item(s) to bring", nargs="+")
2121

2222
parser.add_argument("-s", "--sorted", help="Sort the items", action="store_true")
2323

@@ -29,11 +29,21 @@ def main():
2929
"""Make a jazz noise here"""
3030

3131
args = get_args()
32-
sort_flag = args.on
33-
items = args.str
32+
sort_flag = args.sorted
33+
items = args.items
3434

35-
print(f'flag_arg = "{sort_flag}"')
36-
print(f'positional = "{items}"')
35+
if sort_flag == True:
36+
items.sort()
37+
38+
if len(items) >= 3:
39+
items.insert(-1, "and ")
40+
items_to_bring = ", ".join(items[:-1]) + items[-1]
41+
elif len(items) == 2:
42+
items_to_bring = " and ".join(items)
43+
else:
44+
items_to_bring = items[0]
45+
46+
print("You are bringing " + items_to_bring + ".")
3747

3848

3949
# --------------------------------------------------

0 commit comments

Comments
 (0)