[Tutor] The Card Game
ALAN GAULD
alan.gauld at btinternet.com
Thu Jun 30 23:04:42 CEST 2011
I may be wrong but I thought __cmp__ was deprecated.
In fact I thought it was one oof the things removed in
Python v3....
But I may have just imagined it! :-)
Alan Gauld
Author of the Learn To Program website
http://www.alan-g.me.uk/
________________________________
From: Christopher King <g.nius.ck at gmail.com>
To: Alan Gauld <alan.gauld at btinternet.com>
Cc: tutor at python.org
Sent: Thursday, 30 June, 2011 20:43:29
Subject: Re: [Tutor] The Card Game
I would go with __cmp__ which covers them all. 1 for greater, 0 for equal, -1
for less than.
On Thu, Jun 30, 2011 at 5:35 AM, Alan Gauld <alan.gauld at btinternet.com> wrote:
>"Vincent Balmori" <vincentbalmori at yahoo.com> wrote
>
>I keep getting a Type Error since for the moment since the
>>values of the cards cannot be compared due to their
>>types.
>>
Please send the complete error text sincethat will tell us
where to look etc.
I am thinking of creating a Card_Value class that will give each rank
>and suit a certain value. If there is also a more elegant way of handling
>some of the code in the main section that will be welcome.
>
You can get cards to compare themselves by adding a few
more "magic methods". Then you can do stuff like
if card1 < card2:
# ....
elif card2 > card1:
# ....
else:....
The methods you need to create are
__gt__(), __lt__(), __eq__(), __le__(), __ge__()
for
,<,==,<=,>=
>
operations.
BTW, In your code you have a comparison using = instead of ==.
That will fail.
HTH,
--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/
_______________________________________________
Tutor maillist - Tutor at python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110630/065400d2/attachment-0001.html>
More information about the Tutor
mailing list