From c500424956b04f3945e6f74ac469b67cd00f3e44 Mon Sep 17 00:00:00 2001 From: Martin Thoma Date: Sat, 2 Apr 2022 07:32:09 +0200 Subject: [PATCH] Simplify Python Code print_with_tab / print_with_whitespace is trivial with Python string formatting and was mostly used in only 2 lines. --- 01_Acey_Ducey/python/acey_ducey.py | 20 ++++----- 02_Amazing/python/amazing.py | 13 +++--- 03_Animal/python/animal.py | 4 +- 04_Awari/python/awari.py | 14 +------ 05_Bagels/python/bagels.py | 3 +- 07_Basketball/python/basketball.py | 12 +++--- 10_Blackjack/python/blackjack.py | 13 +++--- 11_Bombardment/python/bombardment.py | 13 +++--- 15_Boxing/python/boxing.py | 4 +- 16_Bug/python/bug.py | 14 ++----- 17_Bullfight/python/bullfight.py | 10 +---- 19_Bunny/python/bunny.py | 20 ++++----- 20_Buzzword/python/buzzword.py | 2 +- 22_Change/python/change.py | 6 +-- 24_Chemist/python/chemist.py | 13 +----- 26_Chomp/python/chomp.py | 4 +- 27_Civil_War/python/Civilwar.py | 18 ++++---- 28_Combat/python/combat.py | 4 +- 29_Craps/python/craps.py | 5 +-- 31_Depth_Charge/python/depth_charge.py | 16 +++---- 32_Diamond/python/diamond.py | 15 +------ 33_Dice/python/dice.py | 4 +- 35_Even_Wins/python/evenwins.py | 6 +-- 38_Fur_Trader/python/furtrader.py | 45 ++++++++++---------- 39_Golf/python/golf.py | 16 ++++--- 40_Gomoko/python/Gomoko.py | 22 +++------- 42_Gunner/python/gunner.py | 12 +++--- 45_Hello/python/hello.py | 19 ++------- 47_Hi-Lo/python/hilo.py | 4 +- 51_Hurkle/python/hurkle.py | 12 ++++-- 52_Kinema/python/kinema.py | 15 +------ 54_Letter/python/letter.py | 16 +------ 58_Love/python/love.py | 2 +- 63_Name/python/name.py | 26 +++--------- 64_Nicomachus/python/nicomachus.py | 15 +------ 66_Number/python/number.py | 16 +------ 67_One_Check/python/onecheck.py | 55 ++++++++++--------------- 72_Queen/python/queen.py | 11 +---- 73_Reverse/python/reverse.py | 4 +- 77_Salvo/python/salvo.py | 6 +-- 82_Stars/python/stars.py | 2 +- 83_Stock_Market/python/Stock_Market.py | 7 +++- 88_3-D_Tic-Tac-Toe/python/qubit.py | 2 +- 89_Tic-Tac-Toe/python/TicTacToe_Hard.py | 4 +- 89_Tic-Tac-Toe/python/tictactoe2.py | 4 +- 95_Weekday/python/weekday.py | 29 ++++--------- 96_Word/python/word.py | 18 ++++---- 47 files changed, 208 insertions(+), 387 deletions(-) mode change 100644 => 100755 96_Word/python/word.py diff --git a/01_Acey_Ducey/python/acey_ducey.py b/01_Acey_Ducey/python/acey_ducey.py index 550d94b2..603e01af 100755 --- a/01_Acey_Ducey/python/acey_ducey.py +++ b/01_Acey_Ducey/python/acey_ducey.py @@ -24,7 +24,6 @@ cards = { def play_game() -> None: - """Play the game""" cash = 100 while cash > 0: print(f"You now have {cash} dollars\n") @@ -64,16 +63,6 @@ def play_game() -> None: def main() -> None: - """Main""" - keep_playing = True - - while keep_playing: - play_game() - keep_playing = input("Try again? (yes or no) ").lower().startswith("y") - print("Ok hope you had fun") - - -if __name__ == "__main__": print( """ Acey-Ducey is played in the following manner @@ -84,4 +73,13 @@ a value between the first two. If you do not want to bet, input a 0 """ ) + keep_playing = True + + while keep_playing: + play_game() + keep_playing = input("Try again? (yes or no) ").lower().startswith("y") + print("Ok hope you had fun") + + +if __name__ == "__main__": main() diff --git a/02_Amazing/python/amazing.py b/02_Amazing/python/amazing.py index bdffe7e7..75d37a20 100644 --- a/02_Amazing/python/amazing.py +++ b/02_Amazing/python/amazing.py @@ -1,7 +1,7 @@ -import random import enum -from typing import List, Tuple +import random from dataclasses import dataclass +from typing import List, Tuple # Python translation by Frank Palazzolo - 2/2021 @@ -77,18 +77,15 @@ EXIT_RIGHT = 2 def main() -> None: - welcome_header() + print_intro() width, length = get_maze_dimensions() maze = build_maze(width, length) maze.display() -def welcome_header() -> None: +def print_intro() -> None: print(" " * 28 + "AMAZING PROGRAM") - print(" " * 15 + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY") - print() - print() - print() + print(" " * 15 + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n\n\n") def build_maze(width: int, length: int) -> Maze: diff --git a/03_Animal/python/animal.py b/03_Animal/python/animal.py index 10e9041f..618ef7d6 100644 --- a/03_Animal/python/animal.py +++ b/03_Animal/python/animal.py @@ -119,7 +119,7 @@ def avoid_void_input(message: str) -> str: return answer -def initial_message() -> None: +def print_intro() -> None: print(" " * 32 + "Animal") print(" " * 15 + "Creative Computing Morristown, New Jersey\n") print("Play ´Guess the Animal´") @@ -133,7 +133,7 @@ def main() -> None: root = Node("Does it swim?", yes_child, no_child) # Main loop of game - initial_message() + print_intro() keep_playing = parse_input("Are you thinking of an animal? ", True, root) == "y" while keep_playing: keep_asking = True diff --git a/04_Awari/python/awari.py b/04_Awari/python/awari.py index d05cb0ec..85669cee 100644 --- a/04_Awari/python/awari.py +++ b/04_Awari/python/awari.py @@ -81,14 +81,6 @@ MAX_HISTORY = 9 LOSING_BOOK_SIZE = 50 -def print_with_tab(space_count: int, msg: str) -> None: - if space_count > 0: - spaces = " " * space_count - else: - spaces = "" - print(spaces + msg) - - def draw_pit(line: str, board, pit_index) -> str: val = board[pit_index] line = line + " " @@ -362,10 +354,8 @@ def player_move(board) -> Tuple[int, bool, int]: def main() -> None: - print_with_tab(34, "AWARI") - print_with_tab(15, "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY") - print() - print() + print(" " * 34 + "AWARI") + print(" " * 15 + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n\n") board = [0] * 14 # clear the board representation global losing_book diff --git a/05_Bagels/python/bagels.py b/05_Bagels/python/bagels.py index 77e8b473..c9c0ca84 100644 --- a/05_Bagels/python/bagels.py +++ b/05_Bagels/python/bagels.py @@ -106,8 +106,7 @@ def build_result_string(num: List[str], guess: str) -> str: def main() -> None: # Intro text print("\n Bagels") - print("Creative Computing Morristown, New Jersey") - print("\n\n") + print("Creative Computing Morristown, New Jersey\n\n") # Anything other than N* will show the rules response = input("Would you like the rules (Yes or No)? ") diff --git a/07_Basketball/python/basketball.py b/07_Basketball/python/basketball.py index f9870ca8..758e5cb9 100644 --- a/07_Basketball/python/basketball.py +++ b/07_Basketball/python/basketball.py @@ -9,7 +9,7 @@ import random from typing import List, Literal, Optional -def explain_keyboard_inputs() -> None: +def print_intro() -> None: print("\t\t\t Basketball") print("\t Creative Computing Morristown, New Jersey\n\n\n") print("This is Dartmouth College basketball. ") @@ -35,7 +35,7 @@ class Basketball: self.shot_choices: List[Literal[0, 1, 2, 3, 4]] = [0, 1, 2, 3, 4] self.z1: Optional[float] = None - explain_keyboard_inputs() + print_intro() self.defense = get_defense_choice(self.defense_choices) @@ -154,7 +154,7 @@ class Basketball: # ball is passed back to you self.ball_passed_back() else: - print("") + print() self.dartmouth_non_jump_shot() else: print("Shot is good.") @@ -268,7 +268,7 @@ class Basketball: self.opponent_ball() else: if random.random() > 0.5: - print("") + print() self.opponent_non_jumpshot() else: print("Pass back to " + self.opponent + " guard.\n") @@ -304,14 +304,14 @@ class Basketball: self.opponent_ball() else: if random.random() > 0.5: - print("") + print() self.opponent_non_jumpshot() else: print("Pass back to " + self.opponent + " guard.\n") self.opponent_ball() else: if random.random() > 0.5: - print("") + print() self.opponent_non_jumpshot() else: print("Pass back to " + self.opponent + " guard\n") diff --git a/10_Blackjack/python/blackjack.py b/10_Blackjack/python/blackjack.py index e4d6fa40..a2c38260 100644 --- a/10_Blackjack/python/blackjack.py +++ b/10_Blackjack/python/blackjack.py @@ -242,7 +242,7 @@ class Game: players.append(Player.new(PlayerType.Player, i)) if get_char_from_user_input("Do you want instructions", ["y", "n"]) == "y": - instructions() + print_instructions() print() return Game(players=players, decks=Decks.new(), games_played=0) @@ -284,7 +284,7 @@ class Game: # turn loop, ends when player finishes their turn while True: clear() - welcome() + print_welcome_screen() print(f"\n\t\t\tGame {game}") print(scores) print(player_hands_message) @@ -407,7 +407,7 @@ STARTING_BALANCE: int = 100 def main() -> None: game: Game - welcome() + print_welcome_screen() # create game game = Game.new( @@ -421,9 +421,7 @@ def main() -> None: char = get_char_from_user_input("Play Again?", ["y", "n"]) -def welcome() -> None: - """prints the welcome screen""" - # welcome message +def print_welcome_screen() -> None: print( """ BLACK JACK @@ -432,8 +430,7 @@ def welcome() -> None: ) -def instructions() -> None: - """prints the instructions""" +def print_instructions() -> None: print( """ THIS IS THE GAME OF 21. AS MANY AS 7 PLAYERS MAY PLAY THE diff --git a/11_Bombardment/python/bombardment.py b/11_Bombardment/python/bombardment.py index 2457a87a..b125987a 100755 --- a/11_Bombardment/python/bombardment.py +++ b/11_Bombardment/python/bombardment.py @@ -4,9 +4,9 @@ from functools import partial from typing import Callable, List, Set -def display_intro() -> None: - print("" * 33 + "BOMBARDMENT") - print("" * 15 + " CREATIVE COMPUTING MORRISTOWN, NEW JERSEY") +def print_intro() -> None: + print(" " * 33 + "BOMBARDMENT") + print(" " * 15 + " CREATIVE COMPUTING MORRISTOWN, NEW JERSEY") print("\n\n") print("YOU ARE ON A BATTLEFIELD WITH 4 PLATOONS AND YOU") print("HAVE 25 OUTPOSTS AVAILABLE WHERE THEY MAY BE PLACED.") @@ -28,7 +28,6 @@ def display_field() -> None: for row in range(5): initial = row * 5 + 1 print("\t".join([str(initial + column) for column in range(5)])) - print("\n" * 9) @@ -128,8 +127,8 @@ ENEMY_PROGRESS_MESSAGES = ( ) -def play() -> None: - display_intro() +def main() -> None: + print_intro() display_field() enemy_positions = generate_enemy_positions() @@ -162,4 +161,4 @@ def play() -> None: if __name__ == "__main__": - play() + main() diff --git a/15_Boxing/python/boxing.py b/15_Boxing/python/boxing.py index f1c2ae59..1943e74e 100755 --- a/15_Boxing/python/boxing.py +++ b/15_Boxing/python/boxing.py @@ -77,7 +77,7 @@ def read_punch_profiles(filepath: Path) -> Dict[Literal[1, 2, 3, 4], PunchProfil return result # type: ignore -def play() -> None: +def main() -> None: print("BOXING") print("CREATIVE COMPUTING MORRISTOWN, NEW JERSEY") print("\n\n") @@ -179,4 +179,4 @@ def play_round(round_number: int, player: Player, opponent: Player) -> None: if __name__ == "__main__": - play() + main() diff --git a/16_Bug/python/bug.py b/16_Bug/python/bug.py index 4d93c158..846002f8 100644 --- a/16_Bug/python/bug.py +++ b/16_Bug/python/bug.py @@ -36,10 +36,6 @@ class State: print_legs(self.legs) -def print_n_whitespaces(n: int) -> None: - print(" " * n, end="") - - def print_n_newlines(n: int) -> None: for _ in range(n): print() @@ -47,7 +43,7 @@ def print_n_newlines(n: int) -> None: def print_feelers(n_feelers: int, is_player: bool = True) -> None: for _ in range(4): - print_n_whitespaces(10) + print(" " * 10, end="") for _ in range(n_feelers): print("A " if is_player else "F ", end="") print() @@ -77,7 +73,7 @@ def print_body(has_tail: bool = False) -> None: def print_legs(n_legs: int) -> None: for _ in range(2): - print_n_whitespaces(5) + print(" " * 5, end="") for _ in range(n_legs): print(" L", end="") print() @@ -156,10 +152,8 @@ def handle_roll(diceroll: Literal[1, 2, 3, 4, 5, 6], state: State) -> bool: def main() -> None: - print_n_whitespaces(34) - print("BUG") - print_n_whitespaces(15) - print("CREATIVE COMPUTING MORRISTOWN, NEW JERSEY") + print(" " * 34 + "BUG") + print(" " * 15 + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY") print_n_newlines(3) print("THE GAME BUG") diff --git a/17_Bullfight/python/bullfight.py b/17_Bullfight/python/bullfight.py index 86a4a080..2bb789e2 100644 --- a/17_Bullfight/python/bullfight.py +++ b/17_Bullfight/python/bullfight.py @@ -3,10 +3,6 @@ import random from typing import Dict, List, Literal, Tuple, Union -def print_n_whitespaces(n: int) -> None: - print(" " * n, end="") - - def print_n_newlines(n: int) -> None: for _ in range(n): print() @@ -73,10 +69,8 @@ def calculate_final_score( def print_header() -> None: - print_n_whitespaces(34) - print("BULL") - print_n_whitespaces(15) - print("CREATIVE COMPUTING MORRISTOWN, NEW JERSEY") + print(" " * 34 + "BULL") + print(" " * 15 + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY") print_n_newlines(2) diff --git a/19_Bunny/python/bunny.py b/19_Bunny/python/bunny.py index 1ebfe792..344180e8 100755 --- a/19_Bunny/python/bunny.py +++ b/19_Bunny/python/bunny.py @@ -8,20 +8,14 @@ with open("data.json") as f: DATA = tuple(json.load(f)) -def display_intro() -> None: - print(tab(33) + "BUNNY") - print(tab(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY") +def print_intro() -> None: + print(" " * 33 + "BUNNY") + print(" " * 15 + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY") print("\n\n") -def tab(column: int) -> str: - """Emulates the TAB command in BASIC. Returns a string with ASCII - codes for setting the cursor to the specified column.""" - return f"\r\33[{column}C" - - -def play() -> None: - display_intro() +def main() -> None: + print_intro() # Using an iterator will give us a similar interface to BASIC's READ # command. Instead of READ, we will call 'next(data)' to fetch the next element. @@ -55,7 +49,7 @@ def play() -> None: # position of a line segment. start = command # Position cursor at start - print(tab(start), end="") + print(" " * start, end="") # The following number, indicates the end of the segment. end = next(data) @@ -67,4 +61,4 @@ def play() -> None: if __name__ == "__main__": - play() + main() diff --git a/20_Buzzword/python/buzzword.py b/20_Buzzword/python/buzzword.py index e5809330..466d01b6 100644 --- a/20_Buzzword/python/buzzword.py +++ b/20_Buzzword/python/buzzword.py @@ -91,7 +91,7 @@ def main() -> None: phrase += section[random.randint(0, len(section) - 1)] print(phrase) - print("") + print() response = input("? ") try: diff --git a/22_Change/python/change.py b/22_Change/python/change.py index 19026857..14ee4d4f 100644 --- a/22_Change/python/change.py +++ b/22_Change/python/change.py @@ -88,17 +88,13 @@ def compute_change() -> None: print(f"{change_in_pennies} PENNY(S)") -def print_thanks() -> None: - print("THANK YOU, COME AGAIN.\n\n") - - def main() -> None: print_header("CHANGE") print_introduction() while True: compute_change() - print_thanks() + print("THANK YOU, COME AGAIN.\n\n") if __name__ == "__main__": diff --git a/24_Chemist/python/chemist.py b/24_Chemist/python/chemist.py index 0975b06d..561f67c3 100644 --- a/24_Chemist/python/chemist.py +++ b/24_Chemist/python/chemist.py @@ -11,15 +11,6 @@ import random MAX_LIVES = 9 -def print_with_tab(space_count: int, msg: str) -> None: - if space_count > 0: - spaces = " " * space_count - else: - spaces = "" - - print(spaces + msg) - - def play_scenario() -> bool: acid_amount = random.randint(1, 50) @@ -58,8 +49,8 @@ def show_ending() -> None: def main() -> None: - print_with_tab(33, "CHEMIST") - print_with_tab(15, "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n\n\n") + print(" " * 33 + "CHEMIST") + print(" " * 15 + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n\n\n") print("THE FICTITIOUS CHEMICAL KRYPTOCYANIC ACID CAN ONLY BE") print("DILUTED BY THE RATIO OF 7 PARTS WATER TO 3 PARTS ACID.") diff --git a/26_Chomp/python/chomp.py b/26_Chomp/python/chomp.py index 28ae2819..59878ca6 100755 --- a/26_Chomp/python/chomp.py +++ b/26_Chomp/python/chomp.py @@ -69,9 +69,9 @@ def play_game(): player = 0 alive = True while alive: - print("") + print() print(cookie.render()) - print("") + print() player += 1 if player > players: player = 1 diff --git a/27_Civil_War/python/Civilwar.py b/27_Civil_War/python/Civilwar.py index 4878bffa..b51b7455 100644 --- a/27_Civil_War/python/Civilwar.py +++ b/27_Civil_War/python/Civilwar.py @@ -1,11 +1,11 @@ +""" +Original game design: Cram, Goodie, Hibbard Lexington H.S. +Modifications: G. Paul, R. Hess (Ties), 1973 +""" import math from typing import List -def tab(n: int) -> str: - return " " * n - - def get_choice(prompt: str, choices: List[str]) -> str: while True: choice = input(prompt) @@ -96,13 +96,9 @@ def main(): salaries = {} ammunition = {} oa = {} - print(tab(26) + "CIVIL WAR") - print(tab(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY") - print() - print() - print() - # Original game design: Cram, Goodie, Hibbard Lexington H.S. - # Modifications: G. Paul, R. Hess (Ties), 1973 + print(" " * 26 + "CIVIL WAR") + print(" " * 15 + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n\n\n") + # Union info on likely confederate strategy sa[1] = 25 sa[2] = 25 diff --git a/28_Combat/python/combat.py b/28_Combat/python/combat.py index d6eed5eb..f8a7a5de 100644 --- a/28_Combat/python/combat.py +++ b/28_Combat/python/combat.py @@ -116,7 +116,7 @@ def attack_second() -> None: num_units = 0 unit_type = 0 - print("") + print() print(" YOU ME") print("ARMY ", end="") print("%-14s%s\n" % (usr_army, cpu_army), end="") @@ -176,7 +176,7 @@ def attack_second() -> None: plane_crash_win = True if not plane_crash_win: - print("") + print() print("FROM THE RESULTS OF BOTH OF YOUR ATTACKS,") if plane_crash_win or ( diff --git a/29_Craps/python/craps.py b/29_Craps/python/craps.py index 9d265e4e..dd01fa5e 100644 --- a/29_Craps/python/craps.py +++ b/29_Craps/python/craps.py @@ -20,10 +20,7 @@ def throw_dice() -> int: def main() -> None: print(" " * 33 + "Craps") - print(" " * 15 + "Creative Computing Morristown, New Jersey") - print() - print() - print() + print(" " * 15 + "Creative Computing Morristown, New Jersey\n\n\n") winnings = 0 print("2,3,12 are losers; 4,5,6,8,9,10 are points; 7,11 are natural winners.") diff --git a/31_Depth_Charge/python/depth_charge.py b/31_Depth_Charge/python/depth_charge.py index cce2984f..20a76d30 100644 --- a/31_Depth_Charge/python/depth_charge.py +++ b/31_Depth_Charge/python/depth_charge.py @@ -1,7 +1,9 @@ -# Original BASIC version as published in Basic Computer Games (1978) -# https://www.atariarchives.org/basicgames/showpage.php?page=55 -# -# Converted to Python by Anson VanDoren in 2021 +""" +Original BASIC version as published in Basic Computer Games (1978) +https://www.atariarchives.org/basicgames/showpage.php?page=55 + +Converted to Python by Anson VanDoren in 2021 +""" import math import random @@ -38,7 +40,7 @@ def get_num_charges() -> Tuple[int, int]: def ask_for_new_game() -> None: answer = input("Another game (Y or N): ") if answer.lower().strip()[0] == "y": - start_new_game() + main() else: print("OK. Hope you enjoyed yourself") exit() @@ -112,10 +114,10 @@ def play_game(search_area, num_charges): ask_for_new_game() -def start_new_game() -> None: +def main() -> None: search_area, num_charges = get_num_charges() play_game(search_area, num_charges) if __name__ == "__main__": - start_new_game() + main() diff --git a/32_Diamond/python/diamond.py b/32_Diamond/python/diamond.py index 88713a88..771c74dc 100644 --- a/32_Diamond/python/diamond.py +++ b/32_Diamond/python/diamond.py @@ -7,14 +7,6 @@ Ported by Dave LeCompte """ -def print_with_tab(space_count: int, msg: str) -> None: - if space_count > 0: - spaces = " " * space_count - else: - spaces = "" - print(spaces + msg) - - def print_diamond(begin_width, end_width, step, width, count) -> None: edgeString = "CC" fill = "!" @@ -38,11 +30,8 @@ def print_diamond(begin_width, end_width, step, width, count) -> None: def main() -> None: - print_with_tab(33, "DIAMOND") - print_with_tab(15, "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY") - print() - print() - print() + print(" " * 33, "DIAMOND") + print(" " * 15, "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n\n\n") print("FOR A PRETTY DIAMOND PATTERN,") print("TYPE IN AN ODD NUMBER BETWEEN 5 AND 21") width = int(input()) diff --git a/33_Dice/python/dice.py b/33_Dice/python/dice.py index 1529a739..3d87c861 100644 --- a/33_Dice/python/dice.py +++ b/33_Dice/python/dice.py @@ -48,7 +48,7 @@ def main() -> None: still_playing = True while still_playing: - print("") + print() n = int(input("How many rolls? ")) # Roll the dice n times @@ -64,7 +64,7 @@ def main() -> None: print(" %-14d%d" % (i, freq[i])) # Keep playing? - print("") + print() response = input("Try again? ") if len(response) > 0 and response.upper()[0] == "Y": # Clear out the frequency list diff --git a/35_Even_Wins/python/evenwins.py b/35_Even_Wins/python/evenwins.py index b18806c7..debad869 100644 --- a/35_Even_Wins/python/evenwins.py +++ b/35_Even_Wins/python/evenwins.py @@ -35,7 +35,7 @@ def serious_error(msg): exit(1) -def welcome_screen(): +def print_intro(): print("Welcome to Even Wins!") print("Based on evenwins.bas from Creative Computing") print() @@ -149,7 +149,7 @@ def game_over(): print("You are the winner! Congratulations!") else: print("The computer wins: all hail mighty silicon!") - print("") + print() def computer_turn(): @@ -211,7 +211,7 @@ def play_game(): def main() -> None: global whose_turn - welcome_screen() + print_intro() while True: choose_first_player() diff --git a/38_Fur_Trader/python/furtrader.py b/38_Fur_Trader/python/furtrader.py index 069b2a9b..82d656dc 100755 --- a/38_Fur_Trader/python/furtrader.py +++ b/38_Fur_Trader/python/furtrader.py @@ -22,12 +22,6 @@ FORT_NEWYORK = 3 FORT_NAMES = ["HOCHELAGA (MONTREAL)", "STADACONA (QUEBEC)", "NEW YORK"] -def print_at_column(column: int, words: str) -> None: - """Print the words at the specified column""" - spaces = " " * column # make a fat string of spaces - print(spaces + words) - - def show_introduction() -> None: """Show the player the introductory message""" print("YOU ARE THE LEADER OF A FRENCH FUR TRADING EXPEDITION IN ") @@ -36,7 +30,7 @@ def show_introduction() -> None: print("FORTS AT WHICH YOU MAY TRADE. THE COST OF SUPPLIES") print("AND THE AMOUNT YOU RECEIVE FOR YOUR FURS WILL DEPEND") print("ON THE FORT THAT YOU CHOOSE.") - print("") + print() def get_fort_choice() -> int: @@ -45,7 +39,7 @@ def get_fort_choice() -> int: prompting the user.""" result = 0 while result == 0: - print("") + print() print("YOU MAY TRADE YOUR FURS AT FORT 1, FORT 2,") print("OR FORT 3. FORT 1 IS FORT HOCHELAGA (MONTREAL)") print("AND IS UNDER THE PROTECTION OF THE FRENCH ARMY.") @@ -70,7 +64,7 @@ def get_fort_choice() -> int: def show_fort_comment(which_fort): """Print the description for the fort""" - print("") + print() if which_fort == FORT_MONTREAL: print("YOU HAVE CHOSEN THE EASIEST ROUTE. HOWEVER, THE FORT") print("IS FAR FROM ANY SEAPORT. THE VALUE") @@ -89,7 +83,7 @@ def show_fort_comment(which_fort): else: print("Internal error #1, fort " + str(which_fort) + " does not exist") sys.exit(1) # you have a bug - print("") + print() def get_yes_or_no(): @@ -116,7 +110,7 @@ def get_furs_purchase(): print("YOUR " + str(MAX_FURS) + " FURS ARE DISTRIBUTED AMONG THE FOLLOWING") print("KINDS OF PELTS: MINK, BEAVER, ERMINE AND FOX.") - print("") + print() for i in range(len(FUR_NAMES)): print("HOW MANY " + FUR_NAMES[i] + " DO YOU HAVE") @@ -130,11 +124,10 @@ def get_furs_purchase(): return results -if __name__ == "__main__": - - print_at_column(31, "FUR TRADER") - print_at_column(15, "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY") - print_at_column(15, "(Ported to Python Oct 2012 krt@krt.com.au)") +def main(): + print(" " * 31 + "FUR TRADER") + print(" " * 15 + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY") + print(" " * 15 + "(Ported to Python Oct 2012 krt@krt.com.au)") print("\n\n\n") game_state = "starting" @@ -155,13 +148,13 @@ if __name__ == "__main__": game_state = "trading" elif game_state == "trading": - print("") + print() print("YOU HAVE $ %1.2f IN SAVINGS" % (player_funds)) print("AND " + str(MAX_FURS) + " FURS TO BEGIN THE EXPEDITION") player_furs = get_furs_purchase() if sum(player_furs) > MAX_FURS: - print("") + print() print("YOU MAY NOT HAVE THAT MANY FURS.") print("DO NOT TRY TO CHEAT. I CAN ADD.") print("YOU MUST START AGAIN.") @@ -178,7 +171,7 @@ if __name__ == "__main__": game_state = "travelling" elif game_state == "travelling": - print("") + print() if which_fort == FORT_MONTREAL: mink_price = ( int((0.2 * random.random() + 0.70) * 100 + 0.5) / 100 @@ -234,7 +227,7 @@ if __name__ == "__main__": ) sys.exit(1) # you have a bug - print("") + print() print("SUPPLIES AT FORT STADACONA COST $125.00.") print("YOUR TRAVEL EXPENSES TO STADACONA WERE $15.00.") player_funds -= 140 @@ -281,7 +274,7 @@ if __name__ == "__main__": ) sys.exit(1) # you have a bug - print("") + print() print("SUPPLIES AT NEW YORK COST $85.00.") print("YOUR TRAVEL EXPENSES TO NEW YORK WERE $25.00.") player_funds -= 105 @@ -296,7 +289,7 @@ if __name__ == "__main__": ermine_value = ermine_price * player_furs[FUR_ERMINE] mink_value = mink_price * player_furs[FUR_MINK] - print("") + print() print("YOUR BEAVER SOLD FOR $%6.2f" % (beaver_value)) print("YOUR FOX SOLD FOR $%6.2f" % (fox_value)) print("YOUR ERMINE SOLD FOR $%6.2f" % (ermine_value)) @@ -304,15 +297,19 @@ if __name__ == "__main__": player_funds += beaver_value + fox_value + ermine_value + mink_value - print("") + print() print( "YOU NOW HAVE $ %1.2f INCLUDING YOUR PREVIOUS SAVINGS" % (player_funds) ) - print("") + print() print("DO YOU WANT TO TRADE FURS NEXT YEAR?") should_trade = get_yes_or_no() if should_trade == "N": sys.exit(0) # STOP else: game_state = "trading" + + +if __name__ == "__main__": + main() diff --git a/39_Golf/python/golf.py b/39_Golf/python/golf.py index 9339c236..1fb28d44 100644 --- a/39_Golf/python/golf.py +++ b/39_Golf/python/golf.py @@ -537,7 +537,7 @@ class Golf: def set_putter_and_stroke(self, strength: float) -> None: putter = self.clubs[self.putt] - self.Stroke((putter[1] * (strength / 10.0)), self.putt) + self.stroke((putter[1] * (strength / 10.0)), self.putt) def ask_gauge(self, c: int) -> None: self.club = self.clubs[c] @@ -555,7 +555,7 @@ class Golf: ) def make_stroke(self, strength: float, c: int) -> None: - self.Stroke((self.club[1] * (strength / 10.0)), c) + self.stroke((self.club[1] * (strength / 10.0)), c) def tee_up(self) -> None: # on the green? automatically select putter @@ -579,7 +579,7 @@ class Golf: else: self.ask("What club do you choose? (1-10)", 1, 10, self.ask_gauge) - def Stroke(self, clubAmt: float, clubIndex: int) -> None: + def stroke(self, clubAmt: float, clubIndex: int) -> None: self.STROKE_NUM += 1 flags = 0b000000000000 @@ -739,7 +739,7 @@ class Golf: if (flags & ace) == ace: print("Hole in One! You aced it.") self.score_card_record_stroke(Ball(0, 0, 0, GameObjType.BALL)) - self.ReportCurrentScore() + self.report_current_score() return if (flags & in_trees) == in_trees: @@ -777,7 +777,7 @@ class Golf: msg = "It's in!" print(msg) self.score_card_record_stroke(Ball(plot.X, plot.Y, 0, GameObjType.BALL)) - self.ReportCurrentScore() + self.report_current_score() return if ((flags & slice_) == slice_) and not ((flags & on_green) == on_green): @@ -829,7 +829,7 @@ class Golf: self.tee_up() - def ReportCurrentScore(self) -> None: + def report_current_score(self) -> None: par = CourseInfo[self.HOLE_NUM].par if len(self.score_card[self.HOLE_NUM]) == par + 1: print("A bogey. One above par.") @@ -986,9 +986,7 @@ class Golf: print(" ") def quit_game(self) -> None: - print("") - print("Looks like rain. Goodbye!") - print("") + print("\nLooks like rain. Goodbye!\n") return def game_over(self) -> None: diff --git a/40_Gomoko/python/Gomoko.py b/40_Gomoko/python/Gomoko.py index db3e02b7..3fe15e4b 100644 --- a/40_Gomoko/python/Gomoko.py +++ b/40_Gomoko/python/Gomoko.py @@ -2,10 +2,6 @@ import random from typing import Any, List, Tuple -def print_n_whitespaces(n: int) -> None: - print(" " * n, end="") - - def print_board(A: List[List[Any]], n: int) -> None: """PRINT THE BOARD""" for i in range(n): @@ -23,25 +19,17 @@ def check_move(_I, _J, _N) -> bool: # 910 def print_banner() -> None: - print_n_whitespaces(33) - print("GOMOKU") - print_n_whitespaces(15) - print("CREATIVE COMPUTING MORRISTOWN, NEW JERSEY") - print() - print() - print() - print("WELCOME TO THE ORIENTAL GAME OF GOMOKO.") - print() + print(" " * 33 + "GOMOKU") + print(" " * 15 + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n\n\n") + print("WELCOME TO THE ORIENTAL GAME OF GOMOKO.\n") print("THE GAME IS PLAYED ON AN N BY N GRID OF A SIZE") print("THAT YOU SPECIFY. DURING YOUR PLAY, YOU MAY COVER ONE GRID") print("INTERSECTION WITH A MARKER. THE OBJECT OF THE GAME IS TO GET") print("5 ADJACENT MARKERS IN A ROW -- HORIZONTALLY, VERTICALLY, OR") print("DIAGONALLY. ON THE BOARD DIAGRAM, YOUR MOVES ARE MARKED") - print("WITH A '1' AND THE COMPUTER MOVES WITH A '2'.") - print() + print("WITH A '1' AND THE COMPUTER MOVES WITH A '2'.\n") print("THE COMPUTER DOES NOT KEEP TRACK OF WHO HAS WON.") - print("TO END THE GAME, TYPE -1,-1 FOR YOUR MOVE.") - print() + print("TO END THE GAME, TYPE -1,-1 FOR YOUR MOVE.\n") def get_board_dimensions() -> int: diff --git a/42_Gunner/python/gunner.py b/42_Gunner/python/gunner.py index ad421e7c..3f46fb39 100644 --- a/42_Gunner/python/gunner.py +++ b/42_Gunner/python/gunner.py @@ -71,12 +71,10 @@ def gunner() -> None: return -if __name__ == "__main__": +def main(): print(" " * 33 + "GUNNER") print(" " * 15 + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY") - print("\n\n\n") - print("YOU ARE THE OFFICER-IN-CHARGE, GIVING ORDERS TO A GUN") print("CREW, TELLING THEM THE DEGREES OF ELEVATION YOU ESTIMATE") print("WILL PLACE A PROJECTILE ON TARGET. A HIT WITHIN 100 YARDS") @@ -85,7 +83,11 @@ if __name__ == "__main__": while True: gunner() - Y = input("TRY AGAIN (Y OR N)? ") - if Y != "Y": + not_again = input("TRY AGAIN (Y OR N)? ").upper() != "Y" + if not_again: print("\nOK. RETURN TO BASE CAMP.") break + + +if __name__ == "__main__": + main() diff --git a/45_Hello/python/hello.py b/45_Hello/python/hello.py index 7293ee28..9afc6572 100644 --- a/45_Hello/python/hello.py +++ b/45_Hello/python/hello.py @@ -12,14 +12,6 @@ import time from typing import Optional, Tuple -def print_with_tab(space_count: int, msg: str) -> None: - if space_count > 0: - spaces = " " * space_count - else: - spaces = "" - print(spaces + msg) - - def get_yes_or_no() -> Tuple[bool, Optional[bool], str]: msg = input() if msg.upper() == "YES": @@ -179,14 +171,9 @@ def happy_goodbye(user_name: str) -> None: def main() -> None: - print_with_tab(33, "HELLO") - print_with_tab(15, "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY") - print() - print() - print() - print("HELLO. MY NAME IS CREATIVE COMPUTER.") - print() - print() + print(" " * 33 + "HELLO") + print(" " * 15 + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n\n\n") + print("HELLO. MY NAME IS CREATIVE COMPUTER.\n\n") print("WHAT'S YOUR NAME?") user_name = input() print() diff --git a/47_Hi-Lo/python/hilo.py b/47_Hi-Lo/python/hilo.py index d0badf73..1c340273 100755 --- a/47_Hi-Lo/python/hilo.py +++ b/47_Hi-Lo/python/hilo.py @@ -5,7 +5,7 @@ MAX_ATTEMPTS = 6 QUESTION_PROMPT = "? " -def play(): +def main(): print("HI LO") print("CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n\n\n") print("THIS IS THE GAME OF HI LO.\n") @@ -50,4 +50,4 @@ def play(): if __name__ == "__main__": - play() + main() diff --git a/51_Hurkle/python/hurkle.py b/51_Hurkle/python/hurkle.py index 62cffecb..4a7bf0f4 100644 --- a/51_Hurkle/python/hurkle.py +++ b/51_Hurkle/python/hurkle.py @@ -1,11 +1,11 @@ #!/usr/bin/env python3 -# -# Ported to Python by @iamtraction + +"""Ported to Python by @iamtraction""" from random import random -def direction(A, B, X, Y): +def direction(A, B, X, Y) -> None: """Prints the direction hint for finding the hurkle.""" print("GO ", end="") @@ -22,7 +22,7 @@ def direction(A, B, X, Y): print() -if __name__ == "__main__": +def main() -> None: print(" " * 33 + "HURKLE") print(" " * 15 + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY") @@ -63,3 +63,7 @@ if __name__ == "__main__": continue print("\n\nLET'S PLAY AGAIN, HURKLE IS HIDING.\n") + + +if __name__ == "__main__": + main() diff --git a/52_Kinema/python/kinema.py b/52_Kinema/python/kinema.py index 65fbd08f..264b6d3d 100644 --- a/52_Kinema/python/kinema.py +++ b/52_Kinema/python/kinema.py @@ -20,14 +20,6 @@ g = 10 EXPECTED_ACCURACY_PERCENT = 15 -def print_with_tab(spaces_count, msg) -> None: - if spaces_count > 0: - spaces = " " * spaces_count - else: - spaces = "" - print(spaces + msg) - - def do_quiz(): print() print() @@ -75,11 +67,8 @@ def ask_player(question, answer): def main() -> None: - print_with_tab(33, "KINEMA") - print_with_tab(15, "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY") - print() - print() - print() + print(" " * 33 + "KINEMA") + print(" " * 15 + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n\n\n") while True: do_quiz() diff --git a/54_Letter/python/letter.py b/54_Letter/python/letter.py index 77c87c7f..50fed9c2 100644 --- a/54_Letter/python/letter.py +++ b/54_Letter/python/letter.py @@ -15,15 +15,6 @@ import random BELLS_ON_SUCCESS = False -def print_with_tab(space_count: int, msg: str) -> None: - if space_count > 0: - spaces = " " * space_count - else: - spaces = "" - - print(spaces + msg) - - def print_instructions() -> None: print("LETTER GUESSING GAME") print() @@ -67,11 +58,8 @@ def play_game(): def main() -> None: - print_with_tab(33, "LETTER") - print_with_tab(15, "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY") - print() - print() - print() + print(" " * 33 + "LETTER") + print(" " * 15 + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n\n\n") print_instructions() diff --git a/58_Love/python/love.py b/58_Love/python/love.py index a15237ba..27764441 100644 --- a/58_Love/python/love.py +++ b/58_Love/python/love.py @@ -108,7 +108,7 @@ def main() -> None: position += length print(line_text) - print("") + print() if __name__ == "__main__": diff --git a/63_Name/python/name.py b/63_Name/python/name.py index 6574a363..3d7eeb1c 100644 --- a/63_Name/python/name.py +++ b/63_Name/python/name.py @@ -7,14 +7,6 @@ Ported by Dave LeCompte """ -def print_with_tab(space_count: int, msg: str) -> None: - if space_count > 0: - spaces = " " * space_count - else: - spaces = "" - print(spaces + msg) - - def is_yes_ish(answer: str) -> bool: cleaned = answer.strip().upper() if cleaned in ["Y", "YES"]: @@ -23,29 +15,21 @@ def is_yes_ish(answer: str) -> bool: def main() -> None: - print_with_tab(34, "NAME") - print_with_tab(15, "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY") - print() - print() - print() + print(" " * 34 + "NAME") + print(" " * 15 + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n\n\n") print("HELLO.") print("MY NAME iS CREATIVE COMPUTER.") name = input("WHAT'S YOUR NAME (FIRST AND LAST)?") print() name_as_list = list(name) reversed_name = "".join(name_as_list[::-1]) - print(f"THANK YOU, {reversed_name}.") - print() + print(f"THANK YOU, {reversed_name}.\n") print("OOPS! I GUESS I GOT IT BACKWARDS. A SMART") - print("COMPUTER LIKE ME SHOULDN'T MAKE A MISTAKE LIKE THAT!") - print() - print() + print("COMPUTER LIKE ME SHOULDN'T MAKE A MISTAKE LIKE THAT!\n\n") print("BUT I JUST NOTICED YOUR LETTERS ARE OUT OF ORDER.") sorted_name = "".join(sorted(name_as_list)) - print(f"LET'S PUT THEM IN ORDER LIKE THIS: {sorted_name}") - print() - print() + print(f"LET'S PUT THEM IN ORDER LIKE THIS: {sorted_name}\n\n") print("DON'T YOU LIKE THAT BETTER?") like_answer = input() diff --git a/64_Nicomachus/python/nicomachus.py b/64_Nicomachus/python/nicomachus.py index abc48293..8cae4946 100644 --- a/64_Nicomachus/python/nicomachus.py +++ b/64_Nicomachus/python/nicomachus.py @@ -15,14 +15,6 @@ Ported by Dave LeCompte import time -def print_with_tab(spaces_count: int, msg: str) -> None: - if spaces_count > 0: - spaces = " " * spaces_count - else: - spaces = "" - print(spaces + msg) - - def get_yes_or_no(): while True: response = input().upper() @@ -62,11 +54,8 @@ def play_game(): def main() -> None: - print_with_tab(33, "NICOMA") - print_with_tab(15, "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY") - print() - print() - print() + print(" " * 33 + "NICOMA") + print(" " * 15 + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n\n\n") print("BOOMERANG PUZZLE FROM ARITHMETICA OF NICOMACHUS -- A.D. 90!") print() diff --git a/66_Number/python/number.py b/66_Number/python/number.py index e58dd20b..350f8173 100644 --- a/66_Number/python/number.py +++ b/66_Number/python/number.py @@ -9,15 +9,6 @@ Ported by Dave LeCompte import random -def print_with_tab(num_spaces: int, msg: str) -> None: - if num_spaces > 0: - spaces = " " * num_spaces - else: - spaces = "" - - print(spaces + msg) - - def print_instructions() -> None: print("YOU HAVE 100 POINTS. BY GUESSING NUMBERS FROM 1 TO 5, YOU") print("CAN GAIN OR LOSE POINTS DEPENDING UPON HOW CLOSE YOU GET TO") @@ -33,11 +24,8 @@ def fnr(): def main() -> None: - print_with_tab(33, "NUMBER") - print_with_tab(15, "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY") - print() - print() - print() + print(" " * 33 + "NUMBER") + print(" " * 15 + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY") print_instructions() diff --git a/67_One_Check/python/onecheck.py b/67_One_Check/python/onecheck.py index b866ebd9..e6a5da68 100644 --- a/67_One_Check/python/onecheck.py +++ b/67_One_Check/python/onecheck.py @@ -1,22 +1,17 @@ -# ONE CHECK +""" +ONE CHECK -# Port to python by imiro +Port to Python by imiro +""" - -def tab(x): - return " " * x +from typing import Tuple def main() -> None: - # Initial instructions - print(tab(30) + "ONE CHECK") - print(tab(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY") - print() - print() - print() - print("SOLITAIRE CHECKER PUZZLE BY DAVID AHL") - print() + print(" " * 30 + "ONE CHECK") + print(" " * 15 + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n\n\n") + print("SOLITAIRE CHECKER PUZZLE BY DAVID AHL\n") print("48 CHECKERS ARE PLACED ON THE 2 OUTSIDE SPACES OF A") print("STANDARD 64-SQUARE CHECKERBOARD. THE OBJECT IS TO") print("REMOVE AS MANY CHECKERS AS POSSIBLE BY DIAGONAL JUMPS") @@ -25,35 +20,29 @@ def main() -> None: print("THE BOARD PRINTED OUT ON EACH TURN '1' INDICATES A") print("CHECKER AND '0' AN EMPTY SQUARE. WHEN YOU HAVE NO") print("POSSIBLE JUMPS REMAINING, INPUT A '0' IN RESPONSE TO") - print("QUESTION 'JUMP FROM ?'") - print() - print("HERE IS THE NUMERICAL BOARD:") - print() + print("QUESTION 'JUMP FROM ?'\n") + print("HERE IS THE NUMERICAL BOARD:\n") while True: for j in range(1, 64, 8): for i in range(j, j + 7): print(i, end=(" " * (3 if i < 10 else 2))) print(j + 7) - print() - print("AND HERE IS THE OPENING POSITION OF THE CHECKERS.") - print() + print("\nAND HERE IS THE OPENING POSITION OF THE CHECKERS.\n") (jumps, left) = play_game() print() - print("YOU MADE " + jumps + " JUMPS AND HAD " + left + " PIECES") - print("REMAINING ON THE BOARD.") - print() + print(f"YOU MADE {jumps} JUMPS AND HAD {left} PIECES") + print("REMAINING ON THE BOARD.\n") if not (try_again()): break - print() - print("O.K. HOPE YOU HAD FUN!!") + print("\nO.K. HOPE YOU HAD FUN!!") -def play_game(): +def play_game() -> Tuple[str, str]: # Initialize board # Give more than 64 elements to accomodate 1-based indexing board = [1] * 70 @@ -71,13 +60,13 @@ def play_game(): while True: print("JUMP FROM", end=" ") - f = input() - f = int(f) + f_str = input() + f = int(f_str) if f == 0: break print("TO", end=" ") - t = input() - t = int(t) + t_str = input() + t = int(t_str) print() # Check legality of move @@ -115,10 +104,10 @@ def play_game(): def try_again(): print("TRY AGAIN", end=" ") - answer = input() - if answer.upper() == "YES": + answer = input().upper() + if answer == "YES": return True - elif answer.upper() == "NO": + elif answer == "NO": return False print("PLEASE ANSWER 'YES' OR 'NO'.") try_again() diff --git a/72_Queen/python/queen.py b/72_Queen/python/queen.py index 201ea5d0..94f377a1 100755 --- a/72_Queen/python/queen.py +++ b/72_Queen/python/queen.py @@ -134,17 +134,10 @@ SAFE_SPOTS: Final[FrozenSet[Tuple[int, int]]] = COMPUTER_SAFE_SPOTS | frozenset( ) -def str_with_tab(indent: int, text: str, uppercase: bool = True) -> str: - """Create a string with ``indent`` spaces followed by ``text``.""" - if uppercase: - text = text.upper() - return " " * indent + text - - def intro() -> None: """Print the intro and print instructions if desired.""" - print(str_with_tab(33, "Queen")) - print(str_with_tab(15, "Creative Computing Morristown, New Jersey")) + print(" " * 33 + "Queen") + print(" " * 15 + "Creative Computing Morristown, New Jersey") print("\n" * 2) if ask("DO YOU WANT INSTRUCTIONS"): print(INSTR_TXT) diff --git a/73_Reverse/python/reverse.py b/73_Reverse/python/reverse.py index b72aa320..7412675b 100755 --- a/73_Reverse/python/reverse.py +++ b/73_Reverse/python/reverse.py @@ -5,7 +5,7 @@ import textwrap NUMCNT = 9 # How many numbers are we playing with? -def play(): +def main(): print("REVERSE".center(72)) print("CREATIVE COMPUTING MORRISTOWN, NEW JERSEY".center(72)) print() @@ -103,6 +103,6 @@ def rules(): if __name__ == "__main__": try: - play() + main() except KeyboardInterrupt: pass diff --git a/77_Salvo/python/salvo.py b/77_Salvo/python/salvo.py index 456ad755..9d709667 100644 --- a/77_Salvo/python/salvo.py +++ b/77_Salvo/python/salvo.py @@ -231,7 +231,7 @@ def print_board(board) -> None: print(" ", end="") for z in range(BOARD_WIDTH): print(f"{z+1:3}", end="") - print("") + print() for x in range(len(board)): print(f"{x+1:2}", end="") @@ -240,7 +240,7 @@ def print_board(board) -> None: print(f"{' ':3}", end="") else: print(f"{board[x][y]:3}", end="") - print("") + print() # place_ship @@ -338,7 +338,7 @@ def initialize_game(): # print out the title 'screen' print("{:>38}".format("SALVO")) print("{:>57s}".format("CREATIVE COMPUTING MORRISTOWN, NEW JERSEY")) - print("") + print() print("{:>52s}".format("ORIGINAL BY LAWRENCE SIEGEL, 1973")) print("{:>56s}".format("PYTHON 3 PORT BY TODD KAISER, MARCH 2021")) print("\n") diff --git a/82_Stars/python/stars.py b/82_Stars/python/stars.py index 00e17bb5..60fe6214 100644 --- a/82_Stars/python/stars.py +++ b/82_Stars/python/stars.py @@ -80,7 +80,7 @@ def main() -> None: player_has_won = False while (guess_number < MAX_GUESSES) and not player_has_won: - print("") + print() guess = get_guess() guess_number += 1 diff --git a/83_Stock_Market/python/Stock_Market.py b/83_Stock_Market/python/Stock_Market.py index 2798cba3..14d1a485 100644 --- a/83_Stock_Market/python/Stock_Market.py +++ b/83_Stock_Market/python/Stock_Market.py @@ -186,8 +186,7 @@ HAVE $10,000 TO INVEST. USE INTEGERS FOR ALL YOUR INPUTS. ) -if __name__ == "__main__": - +def main(): print("\t\t STOCK MARKET") help = input("\nDO YOU WANT INSTRUCTIONS(YES OR NO)? ") @@ -226,3 +225,7 @@ if __name__ == "__main__": print("\nHOPE YOU HAD FUN!!!!") input("") + + +if __name__ == "__main__": + main() diff --git a/88_3-D_Tic-Tac-Toe/python/qubit.py b/88_3-D_Tic-Tac-Toe/python/qubit.py index 4081e70d..61673fd9 100644 --- a/88_3-D_Tic-Tac-Toe/python/qubit.py +++ b/88_3-D_Tic-Tac-Toe/python/qubit.py @@ -300,7 +300,7 @@ class Qubit: print("\n") def humanMove(self, board): - print("") + print() c = "1234" while True: h = input("Your move?\n") diff --git a/89_Tic-Tac-Toe/python/TicTacToe_Hard.py b/89_Tic-Tac-Toe/python/TicTacToe_Hard.py index 400ec7f1..1fc7099e 100644 --- a/89_Tic-Tac-Toe/python/TicTacToe_Hard.py +++ b/89_Tic-Tac-Toe/python/TicTacToe_Hard.py @@ -181,7 +181,7 @@ def display(Game: TicTacToe) -> None: print(line1, "\n\n") -def play() -> None: +def main() -> None: Pick = input("Pick 'X' or 'O' ").strip().upper() if Pick == "O": Game = TicTacToe("O") @@ -219,4 +219,4 @@ def play() -> None: if __name__ == "__main__": - play() + main() diff --git a/89_Tic-Tac-Toe/python/tictactoe2.py b/89_Tic-Tac-Toe/python/tictactoe2.py index 519cd7d3..a7452190 100755 --- a/89_Tic-Tac-Toe/python/tictactoe2.py +++ b/89_Tic-Tac-Toe/python/tictactoe2.py @@ -191,7 +191,7 @@ def prompt_player(board): return move -def play(): +def main(): print(" " * 30 + "TIC-TAC-TOE") print(" " * 15 + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY") print("\n\n") @@ -247,4 +247,4 @@ def play(): if __name__ == "__main__": - play() + main() diff --git a/95_Weekday/python/weekday.py b/95_Weekday/python/weekday.py index 6da78bb2..990332d7 100644 --- a/95_Weekday/python/weekday.py +++ b/95_Weekday/python/weekday.py @@ -16,15 +16,7 @@ import datetime GET_TODAY_FROM_SYSTEM = True -def print_with_tab(space_count: int, s: str) -> None: - if space_count > 0: - spaces = " " * space_count - else: - spaces = "" - print(spaces + s) - - -def get_date_from_user(prompt): +def get_date_from_user(prompt: str): while True: print(prompt) date_str = input() @@ -62,7 +54,7 @@ def previous_day(b): return b - 1 -def is_leap_year(year): +def is_leap_year(year: int) -> bool: if (year % 4) != 0: return False if (year % 100) != 0: @@ -113,7 +105,7 @@ def deduct_time(frac, days, years_remain, months_remain, days_remain): def time_report(msg, years, months, days): leading_spaces = 23 - len(msg) - print_with_tab(leading_spaces, msg + f"\t{years}\t{months}\t{days}") + print(" " * leading_spaces + f"{msg}\t{years}\t{months}\t{days}") def make_occupation_label(years): @@ -147,11 +139,8 @@ def end(): def main() -> None: - print_with_tab(32, "WEEKDAY") - print_with_tab(15, "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY") - print() - print() - print() + print(" " * 32 + "WEEKDAY") + print(" " * 15 + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n\n\n") print("WEEKDAY IS A COMPUTER DEMONSTRATION THAT") print("GIVES FACTS ABOUT A DATE OF INTEREST TO YOU.") print() @@ -226,8 +215,8 @@ def main() -> None: print("***HAPPY BIRTHDAY***") # print report - print_with_tab(23, "\tYEARS\tMONTHS\tDAYS") - print_with_tab(23, "\t-----\t------\t----") + print(" " * 23 + "\tYEARS\tMONTHS\tDAYS") + print(" " * 23 + "\t-----\t------\t----") print(f"YOUR AGE (IF BIRTHDATE)\t{el_years}\t{el_months}\t{el_days}") life_days = (el_years * 365) + (el_months * 30) + el_days + int(el_months / 2) @@ -255,11 +244,9 @@ def main() -> None: # Calculate retirement date e = year + 65 - print_with_tab(16, f"*** YOU MAY RETIRE IN {e} ***") + print(" " * 16 + f"*** YOU MAY RETIRE IN {e} ***") end() if __name__ == "__main__": main() - - # test_harness() diff --git a/96_Word/python/word.py b/96_Word/python/word.py old mode 100644 new mode 100755 index fcb3fc11..8b7f0d51 --- a/96_Word/python/word.py +++ b/96_Word/python/word.py @@ -1,7 +1,10 @@ #!/usr/bin/env python3 -# WORD -# -# Converted from BASIC to Python by Trevor Hobson + +""" +WORD + +Converted from BASIC to Python by Trevor Hobson +""" import random @@ -21,7 +24,7 @@ words = [ ] -def play_game(): +def play_game() -> None: """Play one round of the game""" random.shuffle(words) @@ -54,16 +57,15 @@ def play_game(): if i == j: guess_progress[j] = guess_word[i] print( - "There were", - matches, - "matches and the common letters were... " + common_letters, + f"There were {matches}", + f"matches and the common letters were... {common_letters}", ) print( "From the exact letter matches, you know............ " + "".join(guess_progress) ) if "".join(guess_progress) == guess_word: - print("\nYou have guessed the word. It took", guess_count, "guesses!") + print(f"\nYou have guessed the word. It took {guess_count} guesses!") break elif matches == 0: print("\nIf you give up, type '?' for you next guess.")