Last active
January 12, 2026 18:14
-
-
Save johncongdon/b080db564a37bcc9ffd147f1eec523c8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import csv | |
| import sys | |
| try: | |
| import termios | |
| import tty | |
| WINDOWS = False | |
| except ImportError: | |
| import msvcrt | |
| WINDOWS = True | |
| def get_player_number(): | |
| while True: | |
| try: | |
| player_num = int(input("Enter player number (0 to exit): ")) | |
| return player_num | |
| except ValueError: | |
| print("Please enter a valid number.") | |
| def get_single_char(): | |
| """Get a single character from stdin without pressing Enter""" | |
| if WINDOWS: | |
| return msvcrt.getch().decode('utf-8') | |
| else: | |
| fd = sys.stdin.fileno() | |
| old_settings = termios.tcgetattr(fd) | |
| try: | |
| tty.setraw(sys.stdin.fileno()) | |
| char = sys.stdin.read(1) | |
| finally: | |
| termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) | |
| return char | |
| def get_catcher_value(): | |
| while True: | |
| print("Enter catcher value (0 or 1): ", end='', flush=True) | |
| char = get_single_char() | |
| print(char) # Echo the character | |
| if char in ['0', '1']: | |
| return int(char) | |
| else: | |
| print("Catcher value must be 0 or 1.") | |
| def get_rating_values(): | |
| rating_labels = [ | |
| "Run", "Hitting Mechanics", "Hitting Power", "Ground Balls", | |
| "Fly Balls", "Throwing Accuracy", "Throwing Velocity", | |
| "Pitching Mechanics", "Pitching Velocity" | |
| ] | |
| ratings = [] | |
| for i, label in enumerate(rating_labels): | |
| while True: | |
| print(f"Enter {label} rating (1-5): ", end='', flush=True) | |
| char = get_single_char() | |
| print(char) # Echo the character | |
| if char in ['1', '2', '3', '4', '5']: | |
| ratings.append(int(char)) | |
| break | |
| else: | |
| print("Rating must be between 1 and 5.") | |
| return ratings | |
| def main(): | |
| if len(sys.argv) != 2: | |
| print("Usage: python3 player_data_entry.py <filename>") | |
| print("Example: python3 player_data_entry.py john") | |
| sys.exit(1) | |
| filename = sys.argv[1] + ".csv" | |
| # Create CSV file with headers if it doesn't exist | |
| try: | |
| with open(filename, 'x', newline='') as csvfile: | |
| writer = csv.writer(csvfile) | |
| headers = ['Player_Number', 'Catcher', 'Run', 'Hitting_Mechanics', 'Hitting_Power', | |
| 'Ground_Balls', 'Fly_Balls', 'Throwing_Accuracy', 'Throwing_Velocity', | |
| 'Pitching_Mechanics', 'Pitching_Velocity'] | |
| writer.writerow(headers) | |
| except FileExistsError: | |
| pass # File already exists, continue | |
| print("Player Data Entry System") | |
| print(f"Data will be saved to: {filename}") | |
| print("Enter 0 as player number to exit") | |
| print("-" * 30) | |
| while True: | |
| player_number = get_player_number() | |
| if player_number == 0: | |
| print(f"Data entry complete. Data saved to {filename}") | |
| break | |
| catcher_value = get_catcher_value() | |
| ratings = get_rating_values() | |
| # Write data to CSV | |
| with open(filename, 'a', newline='') as csvfile: | |
| writer = csv.writer(csvfile) | |
| row = [player_number, catcher_value] + ratings | |
| writer.writerow(row) | |
| print(f"Player {player_number} data saved successfully!") | |
| print("-" * 30) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment