I'm pretty new to Python and scripting in general. I've written a small program that allows users to read ASCII art files and choose the color in which it displays. I'm looking for feedback and critiques on how I can improve my code. Here's what I've got so far:
```python
import time
import colorama
from colorama import Fore, Style
color = 'WHITE'
colorvar2 = 'WHITE'
# Reset colors
print(Style.RESET_ALL)
# Startup message
print("Welcome to the ASCII art reader. Please set the path to your ASCII art below.")
# Bold text
print(Style.BRIGHT)
# User-defined file path
path = input(Fore.BLUE + 'Please input the file path to your ASCII art: ')
color = input('Please input your desired color (default: white): ' + Style.RESET_ALL)
# If no ASCII art path specified
if not path:
print(Fore.RED + "No ASCII art path specified, Exiting." + Style.RESET_ALL)
time.sleep(2)
exit()
# If no color specified
if not color:
print(Fore.YELLOW + "No color specified, defaulting to white." + Style.RESET_ALL)
color = 'WHITE'
# Reset colors
print(Style.RESET_ALL)
# Set color variables
color2 = color.upper()
colorvar = getattr(Fore, color2)
# Set user-defined color
print(colorvar)
# Read and print the contents of the .txt file
with open(path) as f:
print(f.read())
# Reset colors
print(Style.RESET_ALL)
# Wait for user input before exit
input("Press any key to exit:")
# Exit the program
exit()
```
6 Answers
You might consider using f-strings for better readability instead of concatenation. For example, change this:
```python
print(Fore.YELLOW + "No color specified, defaulting to white." + Style.RESET_ALL)
```
To:
```python
print(f"{Fore.YELLOW} No color specified, defaulting to white. {Style.RESET_ALL}")
```
You've got some good things going, but let's clean it up a bit. The code is pretty average for a beginner. Sorting your imports and using better variable names would help a lot. Also, consider taking input through command-line arguments instead of stdin. Grouping related code in functions is a great next step as well! Overall, I'd score it a 5/10—good start, but there’s room for improvement!
For better maintainability, try breaking your code into functions for easier updates down the line. Keeping your logic organized will help prevent future headaches!
Make sure to raise specific errors in your main function rather than returning generic ones. That’s typically an antipattern.
You're doing well, and with more practice, your code will be even better. The main takeaway is that you've accomplished what you set out to do, which is impressive for a beginner!
Thanks! I actually made a timer app a few weeks ago, so this isn't my very first one.
Not bad for a first program! Just a couple of things to consider: First, you might want to handle cases where the user inputs an invalid color string for `getattr`, as it could throw an error. Second, consider removing the input call at the end; when running a script, the terminal usually stays open unless you close it yourself. Just a friendly tip!
I hadn't thought about the color input validation, but your point on the input function is interesting. I use a .sh file to run scripts, which might be why I think it needs to stay open.
Good call on fixing the color issue with an array! That sounds like a solid solution.
You might want to use external tools like ChatGPT for real-time feedback on your code. It can provide tailored advice based on what you’ve written.

What’s the best practice for importing libraries then?