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

Skip to content

Commit 0abec30

Browse files
committed
added solution for 11
1 parent eb71cdf commit 0abec30

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

11_bottles_of_beer/bottles.py

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

0 commit comments

Comments
 (0)