Python Forum
AssertionError when trying the sys module in python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
AssertionError when trying the sys module in python
#1
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)
The only stupid person in the world, is the person that doesn't ask questions.
-Someone smart
Reply
#2
see this post which uses 'rich' package for colorizing text https://python-forum.io/thread-38602-pos...#pid163693
Reply
#3
(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.
The only stupid person in the world, is the person that doesn't ask questions.
-Someone smart
Reply
#4
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
rob101 likes this post
Reply
#5
(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.
The only stupid person in the world, is the person that doesn't ask questions.
-Someone smart
Reply
#6
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).
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#7
(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?
rob101 and buran like this post
Reply
#8
(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')
Gribouillis likes this post
The only stupid person in the world, is the person that doesn't ask questions.
-Someone smart
Reply
#9
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.
Reply
#10
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  AssertionError: View function mapping is overwriting an existing endpoint function Zhavi221 7 19,115 Apr-17-2019, 01:07 PM
Last Post: Zhavi221

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020