140 return won, columns, mega_score, deficient_score
141
142 def run_simulation(
143 self, num_spins: int, output_file, print_results: bool = False144 ) -> tuple[float, float]:
145 """
146 Runs a simulation of the spinner for a given number of spins.
36 return False
37
38
39def check_websocket_sync(url):40 return asyncio.get_event_loop().run_until_complete(check_websocket(url))
41
42
26import websockets
27
28
29async def check_websocket(url):30 try:
31 async with websockets.connect(url) as websocket:
32 print(f"connected to {url}")
466 str: The chosen move (e.g., 'd3'), or None if no valid moves are available.
467 """
468 if self.game.current_turn == "B" and self.ai_player_black:
469 move = self.ai_player_black.choose_move()470 if move is None or not self.ai_player_black.is_valid_move(move):
471 print("AI (Black) has no valid moves.")
472 return None
389 Image: The generated board image.
390 """
391 board_copy = [row[:] for row in self.game.board]
392 for move in self.game.get_valid_moves(self.game.current_turn):393 row, col = self.game.position_to_indices(move)
394 board_copy[row][col] = "*"
395 print(" a b c d e f g h\n +----------------")
The local variable name hides the variable defined in the outer scope, making it inaccessible and might confuse.
filename = 'myfile.txt'
def read_file(filename): # This shadows the global `filename`
with open(filename) as file:
return file.readlines()
FILENAME = 'myfile.txt' # renamed global to UPPER_CASE as convention
def read_file(filename):
with open(filename) as file:
return file.readlines()
Another usual suspect of this is when you use the same parameter name inside a function as the global variable you are using. For example:
def run_app(app):
# This `app` shadows the global app...
app.run()
if __name__ == '__main__':
app = MyApp() # This is a global variable!
run_app(app)
To avoid this re-defining of a global, consider not defining app
as a global, but inside a main()
function instead:
def run_app(app):
# There is no longer a global `app` variable.
app.run()
def main():
app = MyApp()
run_app(app)
if __name__ == '__main__':
main()