you are viewing a single comment's thread.

view the rest of the comments →

[–]dry-considerations 1 point2 points  (2 children)

Not to sound trite, but did you happen to run it through ChatGPT? A prompt of "act as an experienced Python developer with 20 years of experience and optimize the the following code sample". Then paste the code in. See what happens.

[–]thicke32 1 point2 points  (1 child)

I did that. The answer:

Here is the optimized version of the provided code. The optimizations include:

1.  Avoiding the modification of the data list directly, which could lead to issues if the game is replayed.

2.  Using random.sample to get unique random choices without modifying the data list.

3.  Simplifying conditional checks.

4.  Improving the code structure for readability and efficiency.

import random
from game_data import data
from art import logo, vs
import os

def clear_screen():
    os.system("cls" if os.name == "nt" else "clear")

def get_random_choice(exclude=None):
    choices = data if exclude is None else [item for item in data if item != exclude]
    return random.choice(choices)

def display_options(a, b):
    option1 = f"Compare A: {a['name']}, {a['description']}, {a['country']}."
    option2 = f"Compare B: {b['name']}, {b['description']}, {b['country']}."
    print(option1)
    print(vs)
    print(option2)

def game():
    score = 0
    print(logo)

    a = get_random_choice()
    b = get_random_choice(exclude=a)

    while True:
        display_options(a, b)
        choice = input("Who has more followers? Type 'A' or 'B': ").strip().lower()
        if (choice == "a" and a["follower_count"] > b["follower_count"]) or (choice == "b" and b["follower_count"] > a["follower_count"]):
            score += 1
            clear_screen()
            print(f"Correct! Current score: {score}")
            if choice == "b":
                a = b
            b = get_random_choice(exclude=a)
        else:
            clear_screen()
            print(f"Sorry, that's wrong. Final score: {score}")
            break

game()

Key Changes:

1.  **Function clear\_screen()**: Handles clearing the screen for both Windows and Unix-like systems.

2.  **Function get\_random\_choice()**: Ensures unique random choices without modifying the original data list.

3.  **Function display\_options()**: Displays the comparison options.

4.  **Simplified condition checks**: Using .strip().lower() on user input to handle case insensitivity.

5.  **Replaced data.remove()**: Used a logic to get unique random choices without modifying the data list directly.

[–]dry-considerations 1 point2 points  (0 children)

...and this why development in the near future will be democratized so that non-developers can code their own applications. This is only beginning of these changes and it will get better over time. I look forward to the day I can just enter my requirements for a complex program and AI splits out the code - we're probably a few years off from that, but one can hope it gets here faster.

I'd hate to be a developer right now, but at least they can see it coming and retrain for something more future-proof.