From 2ca98a4d50a41298edc11b4b9fa71a35adcbf9f8 Mon Sep 17 00:00:00 2001 From: Sara Tahir Date: Thu, 27 Nov 2025 13:00:41 +0000 Subject: [PATCH 1/3] Setting up the virtual env for Pyhton and running first script --- implement-cowsay/cow.py | 5 +++++ implement-cowsay/requirements.txt | 1 + 2 files changed, 6 insertions(+) create mode 100644 implement-cowsay/cow.py create mode 100644 implement-cowsay/requirements.txt diff --git a/implement-cowsay/cow.py b/implement-cowsay/cow.py new file mode 100644 index 000000000..2292fb130 --- /dev/null +++ b/implement-cowsay/cow.py @@ -0,0 +1,5 @@ +import cowsay +import sys +# Join all arguments after the script name into one string +message = " ".join(sys.argv[1:]) or "MOO MOOO" +cowsay.cow(message) diff --git a/implement-cowsay/requirements.txt b/implement-cowsay/requirements.txt new file mode 100644 index 000000000..c6b9ffd0e --- /dev/null +++ b/implement-cowsay/requirements.txt @@ -0,0 +1 @@ +cowsay From f1b0550d72899d41b4329313f0b8fa3c13cc5723 Mon Sep 17 00:00:00 2001 From: Sara Tahir Date: Thu, 27 Nov 2025 14:46:56 +0000 Subject: [PATCH 2/3] A working solution to pass animal names as arguments and make them say things --- implement-cowsay/cow.py | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/implement-cowsay/cow.py b/implement-cowsay/cow.py index 2292fb130..6f2fb9a78 100644 --- a/implement-cowsay/cow.py +++ b/implement-cowsay/cow.py @@ -1,5 +1,23 @@ import cowsay -import sys -# Join all arguments after the script name into one string -message = " ".join(sys.argv[1:]) or "MOO MOOO" -cowsay.cow(message) +import argparse #helps you collect the words typed in command line + +parser = argparse.ArgumentParser(prog="cowsay", description ="Make Animals say things") +parser.add_argument( + "--animal", + choices = (cowsay.char_names), + help = "The animal chosen to be saying things", + default = "cow" + ) +parser.add_argument( + "message", + nargs="+", #tells argparse that this argument can accept one or more values and collect them into a list. + help="The message to say." +) +args = parser.parse_args() + +getattr(cowsay, args.animal)(" ".join(args.message)) #Python built‑in func that get an attribute from an object by name = cowsay module in our case. + + + +#we now need to fetch animals from cowsay.char_names +print(cowsay.char_names) \ No newline at end of file From 975ff65080010d9b897fe85b3ce198beb1326152 Mon Sep 17 00:00:00 2001 From: Sara Tahir Date: Thu, 27 Nov 2025 15:10:11 +0000 Subject: [PATCH 3/3] Refactoring the code --- implement-cowsay/cow.py | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/implement-cowsay/cow.py b/implement-cowsay/cow.py index 6f2fb9a78..f1f69778e 100644 --- a/implement-cowsay/cow.py +++ b/implement-cowsay/cow.py @@ -1,23 +1,28 @@ import cowsay import argparse #helps you collect the words typed in command line -parser = argparse.ArgumentParser(prog="cowsay", description ="Make Animals say things") -parser.add_argument( - "--animal", - choices = (cowsay.char_names), - help = "The animal chosen to be saying things", - default = "cow" +def main(): + # Set up command-line parser + parser = argparse.ArgumentParser( + prog="cowsay", + description="Make animals say things" ) -parser.add_argument( - "message", - nargs="+", #tells argparse that this argument can accept one or more values and collect them into a list. - help="The message to say." -) -args = parser.parse_args() - -getattr(cowsay, args.animal)(" ".join(args.message)) #Python built‑in func that get an attribute from an object by name = cowsay module in our case. - + parser.add_argument( + "--animal", + choices=cowsay.char_names, # fetch supported animals directly from cowsay + default="cow", + help="The animal chosen to be saying things" + ) + parser.add_argument( + "message", + nargs="+", # accept one or more words as message + help="The message to say" + ) + args = parser.parse_args() + # Dynamically call the correct cowsay function + say = getattr(cowsay, args.animal) + say(" ".join(args.message)) -#we now need to fetch animals from cowsay.char_names -print(cowsay.char_names) \ No newline at end of file +if __name__ == "__main__": + main()