diff --git a/projects/BMI_calculator/BMI calculator.py b/projects/BMI_calculator/BMI calculator.py index 571a9c716..a123d893b 100644 --- a/projects/BMI_calculator/BMI calculator.py +++ b/projects/BMI_calculator/BMI calculator.py @@ -5,15 +5,15 @@ def reference_chart(): """ This is a function used to tabulate the data - of the bmi scale for the user, this requries a csv file 'bmi.csv' and two libraries "csv" and "tabulate". - It won't take any arguments and won't return anything + of the BMI scale for the user. This requires a CSV file 'bmi.csv' and two libraries: "csv" and "tabulate". + It won't take any arguments and won't return anything. """ list2 = [] with open("bmi.csv") as file1: list1 = csv.reader(file1) for line in list1: list2.append(line) - print("Here You can take the reference chart \n") + print("Here you can take the reference chart:\n") print(tabulate.tabulate(list2[1:], headers=list2[0], tablefmt="fancy_grid")) @@ -62,18 +62,53 @@ def interpret_bmi(bmi): return f"Your BMI is {bmi}, you are obese (Class III)." -def main(): - reference_chart() +def get_user_input(): + """ + Allow the user to enter height and weight in their preferred units. + Returns height in meters and weight in kilograms. + """ try: - height = float(input("Enter your height in meters: ")) - weight = float(input("Enter your weight in kilograms: ")) - - bmi = calculate_bmi(height, weight) - result = interpret_bmi(bmi) - print(result) + unit_choice = input("Would you like to input height and weight in metric (meters/kg) or imperial (feet/inches/lbs)? (Enter 'metric' or 'imperial'): ").strip().lower() + + if unit_choice == "metric": + height = float(input("Enter your height in meters: ")) + weight = float(input("Enter your weight in kilograms: ")) + + elif unit_choice == "imperial": + feet = float(input("Enter your height (feet): ")) + inches = float(input("Enter additional height (inches): ")) + height = (feet * 0.3048) + (inches * 0.0254) # Convert to meters + weight = float(input("Enter your weight in pounds: ")) * 0.453592 # Convert to kilograms + + else: + print("Invalid choice. Please try again.") + return None, None + + if height <= 0 or weight <= 0: + print("Height and weight must be greater than 0. Please try again.") + return None, None + + return height, weight except ValueError: - print("Invalid input. Please enter numerical values for height and weight.") + print("Invalid input. Please enter numerical values.") + return None, None + + +def main(): + print("Welcome to the BMI Calculator!") + print("This tool helps you calculate your Body Mass Index (BMI) and understand your health category.") + print("------------------------------------------------------\n") + + reference_chart() + + height, weight = get_user_input() + if height is None or weight is None: + return + + bmi = calculate_bmi(height, weight) + result = interpret_bmi(bmi) + print(result) if __name__ == "__main__": diff --git a/projects/Connect Four/main.py b/projects/Connect Four/main.py index 370fcffe7..eec092b99 100644 --- a/projects/Connect Four/main.py +++ b/projects/Connect Four/main.py @@ -3,40 +3,49 @@ import sys import math +# Colors BLUE = (0, 0, 255) BLACK = (0, 0, 0) RED = (255, 0, 0) YELLOW = (255, 255, 0) +# Constants ROW_COUNT = 6 COLUMN_COUNT = 7 +SQUARESIZE = 100 +RADIUS = int(SQUARESIZE / 2 - 5) +WIDTH = COLUMN_COUNT * SQUARESIZE +HEIGHT = (ROW_COUNT + 1) * SQUARESIZE +SIZE = (WIDTH, HEIGHT) + +# Initialize Pygame +pygame.init() +# Font for winner message (smaller size) +myfont = pygame.font.SysFont("monospace", 50) +winner_font = pygame.font.SysFont("monospace", 30) +# Initialize the game board def create_board(): - board = np.zeros((ROW_COUNT, COLUMN_COUNT)) - return board - + return np.zeros((ROW_COUNT, COLUMN_COUNT)) +# Drop piece on the board def drop_piece(board, row, col, piece): board[row][col] = piece - +# Check if a column is a valid location def is_valid_location(board, col): return board[ROW_COUNT - 1][col] == 0 - +# Get the next available row in a column def get_next_open_row(board, col): for r in range(ROW_COUNT): if board[r][col] == 0: return r - -def print_board(board): - print(np.flip(board, 0)) - - +# Check for a winning move def winning_move(board, piece): - # Check horizontal locations for win + # Horizontal for c in range(COLUMN_COUNT - 3): for r in range(ROW_COUNT): if ( @@ -47,7 +56,7 @@ def winning_move(board, piece): ): return True - # Check vertical locations for win + # Vertical for c in range(COLUMN_COUNT): for r in range(ROW_COUNT - 3): if ( @@ -58,7 +67,7 @@ def winning_move(board, piece): ): return True - # Check positively sloped diaganols + # Diagonal positive slope for c in range(COLUMN_COUNT - 3): for r in range(ROW_COUNT - 3): if ( @@ -69,7 +78,7 @@ def winning_move(board, piece): ): return True - # Check negatively sloped diaganols + # Diagonal negative slope for c in range(COLUMN_COUNT - 3): for r in range(3, ROW_COUNT): if ( @@ -79,127 +88,83 @@ def winning_move(board, piece): and board[r - 3][c + 3] == piece ): return True + return False - -def draw_board(board): +# Draw the game board +def draw_board(board, screen): for c in range(COLUMN_COUNT): for r in range(ROW_COUNT): pygame.draw.rect( - screen, - BLUE, - (c * SQUARESIZE, r * SQUARESIZE + SQUARESIZE, SQUARESIZE, SQUARESIZE), + screen, BLUE, (c * SQUARESIZE, r * SQUARESIZE + SQUARESIZE, SQUARESIZE, SQUARESIZE) ) pygame.draw.circle( - screen, - BLACK, - ( - int(c * SQUARESIZE + SQUARESIZE / 2), - int(r * SQUARESIZE + SQUARESIZE + SQUARESIZE / 2), - ), - RADIUS, + screen, BLACK, (int(c * SQUARESIZE + SQUARESIZE / 2), int(r * SQUARESIZE + SQUARESIZE + SQUARESIZE / 2)), RADIUS ) for c in range(COLUMN_COUNT): for r in range(ROW_COUNT): if board[r][c] == 1: pygame.draw.circle( - screen, - RED, - ( - int(c * SQUARESIZE + SQUARESIZE / 2), - height - int(r * SQUARESIZE + SQUARESIZE / 2), - ), - RADIUS, + screen, RED, (int(c * SQUARESIZE + SQUARESIZE / 2), HEIGHT - int(r * SQUARESIZE + SQUARESIZE / 2)), RADIUS ) elif board[r][c] == 2: pygame.draw.circle( - screen, - YELLOW, - ( - int(c * SQUARESIZE + SQUARESIZE / 2), - height - int(r * SQUARESIZE + SQUARESIZE / 2), - ), - RADIUS, + screen, YELLOW, (int(c * SQUARESIZE + SQUARESIZE / 2), HEIGHT - int(r * SQUARESIZE + SQUARESIZE / 2)), RADIUS ) pygame.display.update() +# Show popup winner message +def show_popup(screen, message): + popup = pygame.Surface((400, 200)) + popup.fill(WHITE) + text = winner_font.render(message, True, BLACK) + popup.blit(text, (50, 70)) -board = create_board() -print_board(board) -game_over = False -turn = 0 - -# initalize pygame -pygame.init() + screen.blit(popup, (WIDTH // 2 - 200, HEIGHT // 2 - 100)) + pygame.display.update() -# define our screen size -SQUARESIZE = 100 +# Main game loop +def main(): + board = create_board() + screen = pygame.display.set_mode(SIZE) + pygame.display.set_caption("Connect Four") -# define width and height of board -width = COLUMN_COUNT * SQUARESIZE -height = (ROW_COUNT + 1) * SQUARESIZE + game_over = False + turn = 0 -size = (width, height) + draw_board(board, screen) -RADIUS = int(SQUARESIZE / 2 - 5) + while not game_over: + for event in pygame.event.get(): + if event.type == pygame.QUIT: + pygame.quit() + sys.exit() -screen = pygame.display.set_mode(size) -# Calling function draw_board again -draw_board(board) -pygame.display.update() - -myfont = pygame.font.SysFont("monospace", 75) - -while not game_over: - for event in pygame.event.get(): - if event.type == pygame.QUIT: - sys.exit() - - if event.type == pygame.MOUSEMOTION: - pygame.draw.rect(screen, BLACK, (0, 0, width, SQUARESIZE)) - posx = event.pos[0] - if turn == 0: - pygame.draw.circle(screen, RED, (posx, int(SQUARESIZE / 2)), RADIUS) - else: - pygame.draw.circle(screen, YELLOW, (posx, int(SQUARESIZE / 2)), RADIUS) - pygame.display.update() - - if event.type == pygame.MOUSEBUTTONDOWN: - pygame.draw.rect(screen, BLACK, (0, 0, width, SQUARESIZE)) - # print(event.pos) - # Ask for Player 1 Input - if turn == 0: + if event.type == pygame.MOUSEMOTION: + pygame.draw.rect(screen, BLACK, (0, 0, WIDTH, SQUARESIZE)) posx = event.pos[0] - col = int(math.floor(posx / SQUARESIZE)) + if turn == 0: + pygame.draw.circle(screen, RED, (posx, int(SQUARESIZE / 2)), RADIUS) + else: + pygame.draw.circle(screen, YELLOW, (posx, int(SQUARESIZE / 2)), RADIUS) - if is_valid_location(board, col): - row = get_next_open_row(board, col) - drop_piece(board, row, col, 1) - - if winning_move(board, 1): - label = myfont.render("Player 1 wins!!", 1, RED) - screen.blit(label, (40, 10)) - game_over = True - - # # Ask for Player 2 Input - else: - posx = event.pos[0] - col = int(math.floor(posx / SQUARESIZE)) + if event.type == pygame.MOUSEBUTTONDOWN: + pygame.draw.rect(screen, BLACK, (0, 0, WIDTH, SQUARESIZE)) + col = int(math.floor(event.pos[0] / SQUARESIZE)) if is_valid_location(board, col): row = get_next_open_row(board, col) - drop_piece(board, row, col, 2) + drop_piece(board, row, col, turn + 1) - if winning_move(board, 2): - label = myfont.render("Player 2 wins!!", 1, YELLOW) - screen.blit(label, (40, 10)) + if winning_move(board, turn + 1): + show_popup(screen, f"Player {turn + 1} Wins!") + pygame.time.wait(5000) # Wait for 5 seconds before ending the game game_over = True - print_board(board) - draw_board(board) + turn = (turn + 1) % 2 + draw_board(board, screen) - turn += 1 - turn = turn % 2 + pygame.quit() - if game_over: - pygame.time.wait(3000) +if __name__ == "__main__": + main()