diff --git a/Examples/example_9_getters_setters.py b/Examples/example_9_getters_setters.py index 2f8fad7..8922c13 100644 --- a/Examples/example_9_getters_setters.py +++ b/Examples/example_9_getters_setters.py @@ -28,12 +28,8 @@ def sale_price(self): @sale_price.setter def sale_price(self, sale_price): - if self.sold: - raise MethodNotAllowed('Bike has already been sold') - if sale_price < 0: - raise ValueError("Sale price must be non-negative") - else: - self._sale_price = sale_price + ... # Add some validation + self._sale_price = sale_price def sell(self): """ @@ -66,8 +62,7 @@ def get_test_object(cls): if __name__ == '__main__': bike = Bike.get_test_object() # Class method - print(bike.sale_price) - + print(bike.sale_price) # Calls getter/property bike.sale_price = 1000 # Calls setter print(bike.sale_price) # 1000 diff --git a/README.md b/README.md index 2f68120..80ba153 100644 --- a/README.md +++ b/README.md @@ -15,46 +15,34 @@ Go to https://www.python.org/downloads/ Click the yellow button at the top to download the latest version of Python. -#### On Mac or Linux Follow the prompts and install using the default settings. -#### On Windows -The default settings don't add Python to your PATH -so your computer doesn't know where to look for it when Python runs -(for some inexplicable reason). - -##### If you're just installing Python now -Follow the instructions here: [Windows Python installer instructions](docs/WININSTALL.md) - -##### If you've already installed Python with the default settings -Follow the instructions here: [Add Python to PATH variable in Windows](docs/WINSETPATH.md) - ### 2. Make sure that Python is properly installed -1. Open the *Command Prompt* application in Windows +1. Open the *PowerShell* application in Windows or *Terminal* on Mac or Linux -1. Type `python --version` and press enter - -1. Type `python3 --version` and press enter +1. Type `python --version` and press enter +2. Type `python3 --version` and press enter +3. Type `py --version` and press enter -1. One or both of those commands should print +At least one of those commands should print a Python version of 3.8 or higher (whichever version you just downloaded). - If it doesn't, you have to follow instructions to - [add Python to your PATH variable](docs/WINSETPATH.md). **Note:** -You can now type just the `python` or `python3` command -in *Command Prompt* or *Terminal* -to run the Python interpreter. +You can now type just the `python`, `python3` or `py` command +in *PowerShell* or *Terminal* to run the Python console. You can also run a *.py* file by running -`python filename.py` +` filename.py` -### 3. Download PyCharm (Community Edition) +### 3. Download PyCharm Download here: https://www.jetbrains.com/pycharm/download/ Install, open, and use the default settings. +*Note*: It comes with a 30-day free trial of professional features. +These are not needed for the course and the free features (or the Community edition) will suffice. + ### 4. Download the course files If you're viewing this on GitHub already, stay on this page. Otherwise, go to the GitHub repository: https://github.com/ariannedee/oop-python @@ -75,33 +63,20 @@ for a resource package that has PyCharm shortcuts, links, and a Python 2 to 3 co ## FAQs ### Can I use Python 2? -Yes. There are not many differences for this class. +No, nobody should be using Python 2 anymore. ### Can I use a different code editor besides PyCharm? Jupyter notebooks are not ideal since we'll be working from multiple folders throughout the class. -Other IDEs like VS Code, Atom, and Spyder will work, but they are only recommended if you are already know it +Other IDEs like VS Code, Atom, and Spyder are fine, but they are only recommended if you are already know it and are comfortable navigating to different files and running commands in the command line. -If it has syntax highlighting for Python, that is ideal. ### PyCharm can't find Python 3 -On a Mac: -- Go to **PyCharm** > **Preferences** - -On a PC: -- Go to **File** > **Settings** - -Once in Settings: -1. Go to **Project: oop-python** > **Project Interpreter** -1. Look for your Python version in the Project Interpreter dropdown -1. If it's not there, click **gear icon** > **Add...** -1. In the new window, select **System Interpreter** on the left, and then look for the Python version in the dropdown -1. If it's not there, click the **...** button and navigate to your Python location +1. On the bottom right of PyCharm, click on the Python Interpreter and select one from the list +1. If it's not there, click **Add New Interpreter** > **Add Local Interpreter...** +1. Try to select an existing Python system interpreter and then look for the Python version in the dropdown (exact instructions differ depending on PyCharm version) +1. If it's not there, click the **...** or **folder icon** button and navigate to your Python location - To find where Python is located, [look in these directories](docs/PATH_LOCATIONS.md) - You may have to search the internet for where Python gets installed by default on your operating system - -### Do you offer private Python help? -Yes, email **arianne.dee.studios at gmail.com** if you have any questions -or would like to set up some remote training. diff --git a/Refactoring/refactoring_1_procedural.py b/Refactoring/refactoring_1_procedural.py index ea526e2..26e902b 100644 --- a/Refactoring/refactoring_1_procedural.py +++ b/Refactoring/refactoring_1_procedural.py @@ -2,12 +2,11 @@ Simulate a simple board game. There are 2 players. Each player takes turn rolling a die and moving that number of spaces. -The first person to space 100 wins. +The first person to space 50 wins. """ from random import randint - -def main(): +if __name__ == '__main__': player_1_score = 0 player_2_score = 0 @@ -15,17 +14,15 @@ def main(): player_1_roll = randint(1, 6) player_1_score += player_1_roll print(f"Player 1: {player_1_score} (rolled a {player_1_roll})") - if player_1_score >= 100: + + if player_1_score >= 50: print("Player 1 wins!") - return + break player_2_roll = randint(1, 6) player_2_score += player_2_roll print(f"Player 2: {player_2_score} (rolled a {player_2_roll})") - if player_2_score >= 100: - print("Player 2 wins!") - return - -if __name__ == '__main__': - main() + if player_2_score >= 50: + print("Player 2 wins!") + break \ No newline at end of file diff --git a/Refactoring/refactoring_2_dry.py b/Refactoring/refactoring_2_dry.py index 547c920..a9dd487 100644 --- a/Refactoring/refactoring_2_dry.py +++ b/Refactoring/refactoring_2_dry.py @@ -2,15 +2,12 @@ Simulate a simple board game. There are 2 players. Each player takes turn rolling a die and moving that number of spaces. -The first person to space 100 wins. +The first person to space 50 wins. """ from random import randint - -def run_game(num_players=2, target_score=100): - scores = [] - for i in range(num_players): - scores.append(0) +def play_game(num_players=2, target_score=50): + scores = [0 for i in range(num_players)] while True: for i, score in enumerate(scores): @@ -19,15 +16,15 @@ def run_game(num_players=2, target_score=100): score += roll scores[i] = score print(f"Player {player_num}: {score} (rolled a {roll})") + if score >= target_score: print(f"Player {player_num} wins!") return - if __name__ == '__main__': - print("----- GAME 1 START -----") - run_game(num_players=3, target_score=30) - print("----- GAME 1 END -----") - print("----- GAME 2 START -----") - run_game(num_players=4, target_score=50) - print("----- GAME 2 END -----") + print("------ GAME 1 START ------") + play_game(3, 40) + print("------ GAME 1 END ------") + print("------ GAME 2 START ------") + play_game(4, 30) + print("------ GAME 2 END ------") diff --git a/Refactoring/refactoring_3_abstraction.py b/Refactoring/refactoring_3_abstraction.py index d9c4eec..773b8f8 100644 --- a/Refactoring/refactoring_3_abstraction.py +++ b/Refactoring/refactoring_3_abstraction.py @@ -2,11 +2,10 @@ Simulate a simple board game. There are 2 players. Each player takes turn rolling a die and moving that number of spaces. -The first person to space 100 wins. +The first person to space 50 wins. """ from random import randint - class Player: def __init__(self, num, score=0): self.num = num @@ -27,23 +26,20 @@ def __repr__(self): return f"Player({self.num}, score={self.score})" -def run_game(num_players=2, target_score=100): - players = [] - for i in range(num_players): - players.append(Player(i + 1)) +def play_game(num_players=2, target_score=50): + players = [Player(i + 1) for i in range(num_players)] + while True: for player in players: player.take_turn() - if player.has_won(target_score): print(f"{player} wins!") return - if __name__ == '__main__': - print("----- GAME 1 START -----") - run_game(num_players=3, target_score=30) - print("----- GAME 1 END -----") - print("----- GAME 2 START -----") - run_game(num_players=4, target_score=50) - print("----- GAME 2 END -----") + print("------ GAME 1 START ------") + play_game(3, 40) + print("------ GAME 1 END ------") + print("------ GAME 2 START ------") + play_game(4, 30) + print("------ GAME 2 END ------") diff --git a/Refactoring/refactoring_4_encapsulation/game.py b/Refactoring/refactoring_4_encapsulation/game.py index 8be66df..1436793 100644 --- a/Refactoring/refactoring_4_encapsulation/game.py +++ b/Refactoring/refactoring_4_encapsulation/game.py @@ -4,37 +4,50 @@ class Game: counter = 0 - def __init__(self, num_players=2, target_score=100): + def __init__(self, num_players=2, target_score=50): Game.counter += 1 - self.players = [Player(i + 1) for i in range(num_players)] - self.target_score = target_score self.num = Game.counter + self.target_score = target_score + self.players: list[Player] = [Player(i + 1) for i in range(num_players)] def run(self): - print(f"----- {self} start -----".upper()) + print(f"------ {self} START ------".upper()) + self._take_turns() + print(f"------ {self} END ------".upper()) + def _take_turns(self): while True: for player in self.players: player.take_turn() - if self.game_over(): - print(f"{self.winner()} wins!") - print(f"----- {self} end -----".upper()) + + if self.game_is_over(): + print(f"{self.winner} wins!") + print(self.players) return - def game_over(self): + def player_won(self, player): + return player.score >= self.target_score + + def game_is_over(self): for player in self.players: if player.score >= self.target_score: return True return False + @property def winner(self): - high_score = 0 - cur_winner = None + max_score = 0 + max_player = None + for player in self.players: - if player.score > high_score: - high_score = player.score - cur_winner = player - return cur_winner + if player.score > max_score: + max_score = player.score + max_player = player + + return max_player def __str__(self): return f"Game {self.num}" + + def __repr__(self): + return f"Game({len(self.players)}, {self.target_score})" diff --git a/Refactoring/refactoring_4_encapsulation/main.py b/Refactoring/refactoring_4_encapsulation/main.py index 3c1f9be..a042452 100644 --- a/Refactoring/refactoring_4_encapsulation/main.py +++ b/Refactoring/refactoring_4_encapsulation/main.py @@ -2,12 +2,10 @@ Simulate a simple board game. There are 2 players. Each player takes turn rolling a die and moving that number of spaces. -The first person to space 100 wins. +The first person to space 50 wins. """ from game import Game if __name__ == '__main__': - game1 = Game(num_players=3, target_score=30) - game1.run() - game2 = Game(num_players=4, target_score=50) - game2.run() + Game(3, 40).run() + Game(4, 30).run() diff --git a/Refactoring/refactoring_4_encapsulation/player.py b/Refactoring/refactoring_4_encapsulation/player.py index 91a86b6..9008a83 100644 --- a/Refactoring/refactoring_4_encapsulation/player.py +++ b/Refactoring/refactoring_4_encapsulation/player.py @@ -20,3 +20,9 @@ def __str__(self): def __repr__(self): return f"Player({self.num}, score={self.score})" + + +if __name__ == '__main__': + player = Player(100) + player.take_turn() + player.take_turn() \ No newline at end of file diff --git a/Refactoring/refactoring_5_inheritance/game.py b/Refactoring/refactoring_5_inheritance/game.py index 7420a9b..3d3f857 100644 --- a/Refactoring/refactoring_5_inheritance/game.py +++ b/Refactoring/refactoring_5_inheritance/game.py @@ -1,40 +1,53 @@ -from player import get_player +from player import get_player, Player class Game: counter = 0 - def __init__(self, num_players=2, target_score=100): + def __init__(self, num_players=2, target_score=50): Game.counter += 1 - self.players = [get_player(i + 1) for i in range(num_players)] - self.target_score = target_score self.num = Game.counter + self.target_score = target_score + self.players: list[Player] = [get_player(i + 1) for i in range(num_players)] def run(self): - print(f"----- {self} start -----".upper()) + print(f"------ {self} START ------".upper()) + self._take_turns() + print(f"------ {self} END ------".upper()) + def _take_turns(self): while True: for player in self.players: player.take_turn() - if self.game_over(): - print(f"{self.winner()} wins!") - print(f"----- {self} end -----".upper()) + + if self.game_is_over(): + print(f"{self.winner} wins!") + print(self.players) return - def game_over(self): + def player_won(self, player): + return player.score >= self.target_score + + def game_is_over(self): for player in self.players: if player.score >= self.target_score: return True return False + @property def winner(self): - high_score = 0 - cur_winner = None + max_score = 0 + max_player = None + for player in self.players: - if player.score > high_score: - high_score = player.score - cur_winner = player - return cur_winner + if player.score > max_score: + max_score = player.score + max_player = player + + return max_player def __str__(self): return f"Game {self.num}" + + def __repr__(self): + return f"Game({len(self.players)}, {self.target_score})" diff --git a/Refactoring/refactoring_5_inheritance/main.py b/Refactoring/refactoring_5_inheritance/main.py index 3c1f9be..a042452 100644 --- a/Refactoring/refactoring_5_inheritance/main.py +++ b/Refactoring/refactoring_5_inheritance/main.py @@ -2,12 +2,10 @@ Simulate a simple board game. There are 2 players. Each player takes turn rolling a die and moving that number of spaces. -The first person to space 100 wins. +The first person to space 50 wins. """ from game import Game if __name__ == '__main__': - game1 = Game(num_players=3, target_score=30) - game1.run() - game2 = Game(num_players=4, target_score=50) - game2.run() + Game(3, 40).run() + Game(4, 30).run() diff --git a/Refactoring/refactoring_5_inheritance/player.py b/Refactoring/refactoring_5_inheritance/player.py index ff611fd..6650d88 100644 --- a/Refactoring/refactoring_5_inheritance/player.py +++ b/Refactoring/refactoring_5_inheritance/player.py @@ -23,25 +23,26 @@ def __str__(self): return f"Player {self.num}" def __repr__(self): - return f"Player({self.num}, score={self.score})" + return f"{type(self).__name__}({self.num}, score={self.score})" class LuckyPlayer(Player): - @staticmethod - def _roll(): - return randint(3, 6) + def __init__(self, minimum_roll=3, *args, **kwargs): + print(args) + print(kwargs) + super().__init__(*args, **kwargs) + self.min_roll = minimum_roll + + def _roll(self): + return randint(self.min_roll, 6) def __str__(self): - string = super().__str__() - return string + "*" + base_str = super().__str__() + return f"{base_str}*" def get_player(num): if num == 2: - return LuckyPlayer(num) - return Player(num) - - -if __name__ == '__main__': - lp = LuckyPlayer(10) - print(lp) \ No newline at end of file + return LuckyPlayer(2, num, 20) + else: + return Player(num) diff --git a/docs/WININSTALL.md b/docs/WININSTALL.md deleted file mode 100644 index 7da329b..0000000 --- a/docs/WININSTALL.md +++ /dev/null @@ -1,17 +0,0 @@ -## Installing Python on Windows -The Python installer for Windows doesn't use the optimal default settings. -Follow these instructions when running the installer. -If you have already installed Python with the default settings, -follow the instructions here instead: [add Python to the PATH variable](WINSETPATH.md) - -1. Check the *Add Python to PATH* box -2. Choose the *Customize installation* option - - -3. Keep the default settings and click *Next* - - -4. Check the *Install for all users* box if you want Python accessible by all users. -5. Customize the install location: **C:\Python37** or whatever version number you're installing -6. Click *Install* - diff --git a/docs/WINSETPATH.md b/docs/WINSETPATH.md deleted file mode 100644 index 1a195ab..0000000 --- a/docs/WINSETPATH.md +++ /dev/null @@ -1,26 +0,0 @@ -## Windows set up instruction -If you installed Python with the default options, -you will probably need to add Python to the PATH variable. -This let's your operating system know where to look for the Python executable -when you try running it. - -To add Python to your PATH variable: -1. Find the path of **python.exe** on your system. -[Look in these directories](PATH_LOCATIONS.md) or search for it. - -1. Open *System Properties* and click on the *Advanced* tab - -1. Click on *Environment Variables* - -1. Under *System variables* find and click on the *Path* variable - -1. In edit mode, go to the end of the line and add **;C:\Python37** or whatever folder *python.exe* is in. -Note the semicolon before the path; this will separate it from the previous path. - -Follow the instructions [here](../README.md#4-make-sure-that-python-is-properly-installed) -to check that the PATH variable was set properly. - -If you are having problems: - -Search the internet for "**Add python to path on Windows *10 / Vista / XP / etc***" -to find instructions for your version of Windows. diff --git a/docs/installer_1.png b/docs/installer_1.png deleted file mode 100644 index 0981136..0000000 Binary files a/docs/installer_1.png and /dev/null differ diff --git a/docs/installer_2.png b/docs/installer_2.png deleted file mode 100644 index 524eb42..0000000 Binary files a/docs/installer_2.png and /dev/null differ diff --git a/docs/installer_3.png b/docs/installer_3.png deleted file mode 100644 index 4adb09b..0000000 Binary files a/docs/installer_3.png and /dev/null differ