Skip to content

Commit 54f5523

Browse files
author
joy
committed
#11 joy.y
1 parent 209017e commit 54f5523

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

11_bottles_of_beer/bottles.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Author : NowHappy <rlfmalehd@gmail.com>
4+
Date : 2021-10-13
5+
Purpose: Bottles of beer song
6+
"""
7+
8+
import argparse
9+
10+
11+
# --------------------------------------------------
12+
def get_args():
13+
"""Get command-line arguments"""
14+
15+
parser = argparse.ArgumentParser(
16+
description='Bottles of beer song',
17+
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
18+
19+
parser.add_argument('-n',
20+
'--num',
21+
help='How many bottles',
22+
metavar='number',
23+
type=int,
24+
default=10)
25+
26+
args = parser.parse_args()
27+
28+
if args.num < 1:
29+
parser.error(f'--num "{args.num}" must be greater than 0')
30+
31+
return args
32+
33+
34+
# --------------------------------------------------
35+
def verse(bottle):
36+
"""Sing a verse"""
37+
38+
bottle_str = str(bottle)
39+
s1 = 's' if bottle > 1 else ''
40+
s2 = 's' if bottle != 2 else ''
41+
next_bottle = str(bottle - 1) if bottle > 1 else 'No more'
42+
43+
return '\n'.join([
44+
f'{bottle_str} bottle{s1} of beer on the wall,',
45+
f'{bottle_str} bottle{s1} of beer,',
46+
'Take one down, pass it around,',
47+
# f'{str(bottle - 1)} bottle{s2} of beer on the wall!' if bottle > 1 else 'No more bottles of beer on the wall!'
48+
f'{next_bottle} bottle{s2} of beer on the wall!'
49+
])
50+
51+
52+
# --------------------------------------------------
53+
def test_verse():
54+
"""Test verse"""
55+
56+
last_verse = verse(1)
57+
assert last_verse == '\n'.join([
58+
'1 bottle of beer on the wall,', '1 bottle of beer,',
59+
'Take one down, pass it around,',
60+
'No more bottles of beer on the wall!'
61+
])
62+
63+
two_bottles = verse(2)
64+
assert two_bottles == '\n'.join(['2 bottles of beer on the wall,', '2 bottles of beer,',
65+
'Take one down, pass it around,', '1 bottle of beer on the wall!'
66+
])
67+
68+
69+
# --------------------------------------------------
70+
def main():
71+
"""Make a jazz noise here"""
72+
73+
args = get_args()
74+
# for i in range(args.num, 0, -1):
75+
# print(verse(i))
76+
# print() if i > 1 else None
77+
print('\n\n'.join(map(verse, range(args.num, 0, -1))))
78+
79+
80+
# --------------------------------------------------
81+
if __name__ == '__main__':
82+
main()

0 commit comments

Comments
 (0)