0

I'm creating a text-based blackjack game in Python 3, and one feature I've added is 'ASCII-art' illustrations of cards. This involves a string containing UTF characters which depict a single card:

"╔═════════╗",
"║%s%s       ║" % (value, space),
"║         ║",
"║         ║",
"║         ║",
"║    %s    ║" % (suit),
"║         ║",
"║         ║",
"║         ║",
"║       %s%s║" % (space,value),
"╚═════════╝"

The outline of the card uses Unicode characters, as does the suit variable, which can take one of four characters; ♦ (\u2666), ♣ (\u2663), ♠ (\u2660), ♥ (\u2665).

When I run my program inside the python IDLE, the output is fine.

   ╔═════════╗   ╔═════════╗      
   ║4        ║   ║10       ║      
   ║         ║   ║         ║    __      _  _    
   ║         ║   ║         ║   /_ |    | || |   
   ║         ║   ║         ║    | |    | || |_  
   ║    ♥    ║   ║    ♣    ║    | |    |__   _| 
   ║         ║   ║         ║    | |       | |   
   ║         ║   ║         ║    |_|       |_|   
   ║         ║   ║         ║      
   ║        4║   ║       10║      
   ╚═════════╝   ╚═════════╝      

When I run it from file explorer and so in Shell (only trying to print Four of Hearts), I get the error;

File "C:\Python34\lib\encodings\cp850.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_map)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u2665' in position 8: character maps to (undefined)

I've tried using the Unicode code points in different ways but none seem to work. I looked into my Python's encodings folder which was referenced in the error, and found there was no appearance of codes for my selected characters. Yes, I have put that UTF-8 declaration at the top of my code.

I'm confused because the characters are able to show up in the IDLE but not the Shell.

What is causing this, and how do I fix it so that the characters display without error?

Python 3.4.2, Windows 64-bit

oisinvg
  • 449
  • 2
  • 6
  • 17

1 Answers1

0

Your problem is the same as here: python: unicode in Windows terminal, encoding used?

Windows terminal "cmd.exe" is not unicode compatible. You have to switch to another terminal for your program to work. There are some examples in the other topic. (I suppose you use Windows due to paths in error message)

If you have a Linux computer or VM around, it should work out of the box on it!

Elektordi
  • 270
  • 2
  • 7