Python Forum

Full Version: AssertionError when trying the sys module in python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I was trying to make it so that it wrote the variable 'hp' in red.
import sys
color = sys.stdout.shell
hp = 100
color.write(hp, 'COMMENT')
Got this error.
Error:
Traceback (most recent call last): File "C:\Users\Wannes\survivalor 1.6 The Combat Update Klad.py", line 406, in <module> color.write(hp, 'COMMENT') AssertionError
I am using the IDLE. What is wrong?
Should I just use 'print' for variables and 'color.write' for anything else? Or can this be fixed?
(Any onther method for color, like ANSI, colorama, or colored doesn't work in IDLE, on my computer)
see this post which uses 'rich' package for colorizing text https://python-forum.io/thread-38602-pos...#pid163693
(Nov-20-2022, 02:52 PM)Larz60+ Wrote: [ -> ]see this post which uses 'rich' package for colorizing text https://python-forum.io/thread-38602-pos...#pid163693

Does that work in IDLE? Because anything for color except sys doesn't work on IDLE, as far as I know.
Quote:Does that work in IDLE? Because anything for color except sys doesn't work on IDLE, as far as I know.
My opinion:
Get rid of IDLE and pick a real IDE.
Even though IDLE is packaged with python, it's not a good, nor in my opinion a real IDE.

VSCode is very good and used by many on this forum, as is PyCharm
to install VSCode see: https://python-forum.io/thread-12237.html
(Nov-20-2022, 03:09 PM)Larz60+ Wrote: [ -> ]
Quote:Does that work in IDLE? Because anything for color except sys doesn't work on IDLE, as far as I know.
My opinion:
Get rid of IDLE and pick a real IDE.
Even though IDLE is packaged with python, it's not a good, nor in my opinion a real IDE.

VSCode is very good and used by many on this forum, as is PyCharm
to install VSCode see: https://python-forum.io/thread-12237.html
I will not download anything else. I already have enough downloads everyone tells me to get and I barely use them. Also, IDLE is a completely functional IDE and I will not get rid of it. I don't care what your opinion is on IDLE. Did I ask?
I already found a solution, so I won't use 'Rich' or anything else.
Larz60 Wrote:My opinion:
Get rid of IDLE and pick a real IDE.
Even though IDLE is packaged with python, it's not a good, nor in my opinion a real IDE.

VSCode is very good and used by many on this forum, as is PyCharm
to install VSCode see: https://python-forum.io/thread-12237.html

You can also get the Free/Libre Open Source Software Binaries of VS Code. I've not yet tried this, as I've been using the an different IDE, but for anyone else that may be interested (the thread OP clearly is not).
(Nov-20-2022, 05:19 PM)BliepMonster Wrote: [ -> ]I already found a solution, so I won't use 'Rich' or anything else.
Can you share your solution to print with colors in the IDLE IDE?
(Nov-20-2022, 07:04 PM)Gribouillis Wrote: [ -> ]
(Nov-20-2022, 05:19 PM)BliepMonster Wrote: [ -> ]I already found a solution, so I won't use 'Rich' or anything else.
Can you share your solution to print with colors in the IDLE IDE?

The solution to printing text:
import sys
color = sys.stdout.shell
color.write('Anything you want', 'STRING')
color.write('can go here\n', 'STRING')
color.write('Just remember to end the line with the \\n', 'STRING')
Output:
Anything you want can go here Just remember to end the line with the \n
(In the string color)
Now, this doesn't work for variables. For that, you can just use print() with end=' ' to keep it on the same line. It isn't as good as color, but it works.
import sys
color = sys.stdout.shell
color.write('This', 'STRING')
print('Is on the same line', end = ' ')
color.write('As this', 'STRING')
This is interesting, it actually works to write text in green, blue or red and other colors of the syntax highlighting configuration. The underlying mechanism is the one that allows to print in colors in a Tkinter text window. Perhaps it could be possible to modify the code to print in any color in IDLE. It seems that a lot of the magic happens in the file colorizer.py of idlelib.

Nice idea, however there are not so many applications. Printing in color in a console is often an unnecessary gadget that just makes your code more complex.
The main problem I have with IDLE, other than it having a terrible editor, is that many programs run differently when run in IDLE. If you only ever run your programs from an IDE and the IDE is always IDLE that is not a problem, but that is quite a limitation.
Pages: 1 2