What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Skype & Technology » Tech Talk » unicode in python

unicode in python
Author: Message:
Jarrod
Veteran Member
*****

Avatar
woot simpson

Posts: 1304
Reputation: 20
– / Male / Flag
Joined: Sep 2006
O.P. unicode in python
code:
class card(object):


    heart = u"\2665"
    dimo = u"\u2666"
    spade = u"\u2664"
    club = u"\u2663"
   
    """ A playing card. """
    RANKS = ["A", "2", "3", "4", "5", "6", "7",
             "8", "9", "10", "J", "Q", "K"]
    SUITS = [heart, dimo, club, spade]

    def __init__(self, rank, suit,):            ####defines suits and values
        self.rank = rank
        self.suit = suit

    def __str__(self):                          ####define display string
        rep = self.rank + self.suit
        return rep

class hand (object):
    """A Hand of blackjack cards"""

    def __init__(self):
        self.cards = []

    def __str__(self):
        if self.cards:
            rep = ""
            for card in self.cards:
                rep += str(card) + " "
        else:
            rep = "<empty>"
        return rep

    def clear(self):
        self.cards = [ ]

    def add(self, card):
        self.cards.append(card)

    def give(self, card, other_hand):
        self.cards.remove(card)
        other_hand.add(card)

class deck(hand):
    """ a nice shiny new shiny deck of cards"""

    def pop(self):                           ####lets fill this deck
        for suit in card.SUITS:
            for rank in card.RANKS:
                self.add(card(rank, suit))

    def shuffy(self):                        ###suffle the deck. thank god for random.py
        import random
        random.shuffle(self.cards)

    def deal(self, hands = 2, per_hand = 1):        ###get cards out of the deck into my hand
        for rounds in range(per_hand):
            for hand in hands:
                if self.cards:
                    top_card = self.cards[0]
                    self.give(top_card, hand)
                else:
                    print "the deck has ended"
                   
class priv_card(card):
    """a cards that cant be seen :P"""
    def __str__(self):
        return "!!unprintable!!"
   
class pos_card(card):
    """a card which is face up or down, but either up or down"""
   
    def __init__(self, rank, suit, face_up = True):
        super(pos_card, self).__init__(rank, suit)
        self.is_face_up = face_up
       
    def __str__(self):
        if self.is_face_up:
            rep = super(pos_card, self).__str__()
        else:
            rep = "XX"
        return rep

    def flip(self):
        self.is_face_up = not self.is_face_up
       
   
       
       

##############################################

dek = deck()                            ##############make the dek

dek.pop()                               ##############fill the dek

dek.shuffy()                             ##############shuffle the dek

hnd1 = hand()                           ##############make hand 1

hnd2 = hand()                           ##############make hand 2

###define hands
hands  = [hnd1, hnd2]

##################################################


#################################################
#populate hands
#################################################
dek.deal(hands, per_hand = 2)
#################################################
print "hand1: ", hnd1
print "hand2: ", hnd2
print "remaining cards:\n", dek

##test loop
#for item in dek.cards:
#    print item
####




#############################test loop#################
######################################prints the dek###
#for item in dek.cards:
#    print item



#card1 = card(rank = "A", suit = "c")
#hand_me = hand()
#print hand_me
#hand_me.add(card1)
#print hand_me



quote:
Originally posted by python idle

hand1: 
Traceback (most recent call last):
  File "J:\_mycards1.py", line 125, in ?
    print "hand1: ", hnd1
  File "J:\_mycards1.py", line 40, in __str__
    rep += str(card) + " "
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2663' in position 1: ordinal not in range(128)

so how do i get it to print the unicode characters???
it works if you don't use unicode and use a letter corrosponding to the suit but it looks better this way

[Image: 5344.png]
[Image: sig.png]

A.k.a. The Glad Falconer














04-01-2008 08:31 AM
Profile E-Mail PM Find Quote Report
« Next Oldest Return to Top Next Newest »

Messages In This Thread
unicode in python - by Jarrod on 04-01-2008 at 08:31 AM
RE: unicode in python - by somelauw on 04-01-2008 at 12:16 PM
RE: unicode in python - by Jarrod on 04-02-2008 at 06:15 AM
RE: unicode in python - by somelauw on 04-02-2008 at 11:58 AM


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On