Use a GPT (Phind or Copilot) to generate a sample script
Explore argparse features
Explore classes in Python
Run some tests from the command line
Create issues in GitHub on stuff that needs to be done
This was my prompt:
Create a small python script that uses argparse to read a --hand paramenter and play paper/scissors/rock - and then chooses a random hand and print our if the script or the input parameter won.
Rock beats scissors
Paper beats rock
Scissors beats paper
Please exemplify the use of a class hand and class function hand.play
And this is how Phind replied:
Sure, I'll create a Python script that uses argparse to accept a --hand parameter for choosing between "paper", "scissors", or "rock". The script will determine the winner based on the rules you've provided and print out who wins: the script's choice or the input parameter.
Here's how you can implement this:
import argparse
import random
class Hand:
def __init__(self, name):
self.name = name
def play(self):
# Define the winning logic
outcomes = {
"paper": ["rock"],
"scissors": ["paper"],
"rock": ["scissors"]
}
# Determine the opponent's hand
opponent = random.choice(["paper", "scissors", "rock"])
# Check if the hand wins
if opponent in outcomes[self.name]:
return f"{self.name} wins against {opponent}!"
elif self.name == opponent:
return f"It's a tie Both {self.name} and {opponent} played."
else:
return f"{opponent} wins against {self.name}."
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Play a game of Rock, Paper, Scissors.")
parser.add_argument("--hand", type=str, choices=["paper", "scissors", "rock"], help="Your hand ('paper', 'scissors', or 'rock')")
args = parser.parse_args()
# Choose a random hand for the script
script_hand = Hand(random.choice(["paper", "scissors", "rock"]))
print(f"You chose: {args.hand}")
print(f"The script chose: {script_hand.name}")
print(script_hand.play())
To run this script, save it to a file, for example, rps_game.py. Then, you can execute it from the command line with the --hand parameter set to either "paper", "scissors", or "rock". Here's how you would do it:
python rps_game.py --hand scissors
This will print out what both the script and the user chose, and then it will determine and print the outcome of the game according to the rules you specified.
Hmm 🤔 OK! It's a script- Let's see if it works!
Start by creating a file in a src folder and then create a file rps_game.py and paste the content from above into it.
git add, commit
Try the following executions from the terminal (on at a time)
pipenv run python rps_game.py --hand scissors
pipenv run python rps_game.py --hand rock
pipenv run python rps_game.py --hand paper
pipenv run python rps_game.py --hand this-is-not-a-valid-hand
pipenv run python rps_game.py
pipenv run python rps_game.py --help
pipenv run python rps_game.py --hand
pipenv run python rps_game.py --notaswitch
pipenv run python rps_game.py --hand rock
pipenv run python rps_game.py --hand rock
pipenv run python rps_game.py --hand rock
[!TIP]
Maybe this would have been easier in a pipenv shell?
What's your verdict? Is this a good script?
If you found any issues with this script, things that need to be corrected or fixed then create the issues in your own repo. One issue per finding!
Wrap up this branch - deliver to main - you know how!
Jeg vil mene der mangler "src/" i stien på de commands der skal køre scriptet, eftersom scriptet rps_game.py lægges ind i en mappe kaldet "src".
Men måske det er ment til at være endnu en udfordring 😄
This was my prompt:
And this is how Phind replied:
Hmm 🤔 OK! It's a script- Let's see if it works!
Start by creating a file in a
src
folder and then create a filerps_game.py
and paste the content from above into it.git
add
,commit
Try the following executions from the terminal (on at a time)
What's your verdict? Is this a good script?
If you found any issues with this script, things that need to be corrected or fixed then create the issues in your own repo. One issue per finding!
Wrap up this branch - deliver to
main
- you know how!