Skip to content

Commit bd4f33e

Browse files
Code added into main function as per Python best practice
1 parent 7d1e4b7 commit bd4f33e

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

implement-cowsay/cow.py

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
11
import cowsay
22
import argparse
33

4-
# Get dynamic list of animals from the cowsay library
5-
animals = cowsay.char_names
4+
def main():
5+
# Get dynamic list of animals from the cowsay library
6+
animals = cowsay.char_names
67

7-
parser = argparse.ArgumentParser(
8-
prog="cowsay",
9-
description="Make animals say things"
10-
)
8+
parser = argparse.ArgumentParser(
9+
prog="cowsay",
10+
description="Make animals say things"
11+
)
1112

12-
parser.add_argument("--animal", choices=animals, help="The animal to be saying things.")
13-
parser.add_argument("message", nargs="+", help="The message to say.")
13+
parser.add_argument("--animal", choices=animals, help="The animal to be saying things.")
14+
parser.add_argument("message", nargs="+", help="The message to say.")
1415

15-
args = parser.parse_args()
16+
args = parser.parse_args()
1617

17-
# Join all message words into a single string
18-
message = " ".join(args.message)
18+
# Join all message words into a single string
19+
message = " ".join(args.message)
1920

21+
if args.animal:
22+
cowsay.char_funcs[args.animal](message)
23+
else:
24+
cowsay.cow(message)
2025

21-
if args.animal:
22-
cowsay.char_funcs[args.animal](message)
23-
else:
24-
# if no animal is provided, use default
25-
cowsay.cow(message)
26-
26+
if __name__ == "__main__":
27+
main()

0 commit comments

Comments
 (0)