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

Skip to content

Commit b1c40a2

Browse files
committed
Pass all tests
1 parent c53d869 commit b1c40a2

File tree

1 file changed

+26
-40
lines changed

1 file changed

+26
-40
lines changed

08_apples_and_bananas/apples.py

Lines changed: 26 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,69 +2,55 @@
22
"""
33
Author : fleide <fleide@localhost>
44
Date : 2021-01-27
5-
Purpose: Rock the Casbah
5+
Purpose: Chapter 08 - Find and replace strings
66
"""
77

88
import argparse
9-
9+
import os
1010

1111
# --------------------------------------------------
1212
def get_args():
1313
"""Get command-line arguments"""
1414

1515
parser = argparse.ArgumentParser(
16-
description='Rock the Casbah',
16+
description='Apples and Bananas',
1717
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
1818

19-
parser.add_argument('positional',
20-
metavar='str',
21-
help='A positional argument')
22-
23-
parser.add_argument('-a',
24-
'--arg',
25-
help='A named string argument',
26-
metavar='str',
19+
parser.add_argument('text',
20+
metavar='text',
2721
type=str,
28-
default='')
22+
help='Input text or file')
2923

30-
parser.add_argument('-i',
31-
'--int',
32-
help='A named integer argument',
33-
metavar='int',
34-
type=int,
35-
default=0)
24+
parser.add_argument('-v',
25+
'--vowel',
26+
help='The vowel to substitute, default (a)',
27+
metavar='vowel',
28+
type=str,
29+
choices=['a','e','i','o','u'],
30+
default='a')
3631

37-
parser.add_argument('-f',
38-
'--file',
39-
help='A readable file',
40-
metavar='FILE',
41-
type=argparse.FileType('rt'),
42-
default=None)
32+
args = parser.parse_args()
4333

44-
parser.add_argument('-o',
45-
'--on',
46-
help='A boolean flag',
47-
action='store_true')
34+
if os.path.isfile(args.text):
35+
args.text = open(args.text).read().rstrip()
4836

49-
return parser.parse_args()
37+
return args
5038

5139

5240
# --------------------------------------------------
5341
def main():
54-
"""Make a jazz noise here"""
42+
"""Find and replace strings"""
5543

5644
args = get_args()
57-
str_arg = args.arg
58-
int_arg = args.int
59-
file_arg = args.file
60-
flag_arg = args.on
61-
pos_arg = args.positional
45+
text= args.text
46+
v = args.vowel
47+
48+
vowels=['a','e','i','o','u']
49+
for vowel in vowels:
50+
text = text.replace(vowel,v)
51+
text = text.replace(vowel.upper(),v.upper())
6252

63-
print(f'str_arg = "{str_arg}"')
64-
print(f'int_arg = "{int_arg}"')
65-
print('file_arg = "{}"'.format(file_arg.name if file_arg else ''))
66-
print(f'flag_arg = "{flag_arg}"')
67-
print(f'positional = "{pos_arg}"')
53+
print(text)
6854

6955

7056
# --------------------------------------------------

0 commit comments

Comments
 (0)