3

My python wont output ANSI colours, however when i ran it on repl it outputted fine, do you know what is wrong?

from collections import Counter

print("Welcome, this program calculates the range, mean, median and mode of the numbers you give us!!")
print("\nNow please choose how many numbers you want to calculate range, mean, median and mode")
number_of_numbers = int(input("->"))

print("\nNow is time to choose your numbers")
numbers = []
for i in range(1,number_of_numbers + 1):
    print("\nNumber",i,":")
    num = int(input("->"))
    numbers.append(num)

data = Counter(numbers) 
get_mode = dict(data) 
mode = [k for k, v in get_mode.items() if v == max(list(data.values()))]
nof = len(numbers)

numbers.sort()

print("\n\u001b[32mRange:",numbers[nof - 1] - numbers[0])
print("\n\u001b[32mMean:",sum(numbers)/nof)
print("\n\u001b[32mMode:",', '.join(map(str, mode)))

if nof % 2 == 0:
    median1 = numbers[nof//2]
    median2 = median1 - 1
    median = (median1 + median2)/2
else:
    median = numbers[nof//2]
    print("\nMedian:",median)
Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105

2 Answers2

0

As mentioned here, if you're using Windows you should add import os and os.system('color') to parse the ANSI colors.

Shir
  • 1,571
  • 2
  • 9
  • 27
0

Python is not the culprit here. You have something (here a Python script, meaning some Python source code and a Python interpretor) that outputs bytes sequences. And no doubt that the correct sequences are emitted as you say however when i ran it on repl it outputted fine.

The component in charge of interpreting the ANSI color sequences is the terminal or whatever application is in charge of the display. It can be a term window on Linux, or a console on Windows, or the IDE itself, if you execute your script under IDLE or Pycharm.

And then anything can happen... The term family is normally fine, as are good emulators like Putty. For the Windows console, it can depend on the Windows version, and the configuration of the console. For the IDEs, better is to not expect them to process ANSI escape sequences.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252