[Tutor] Okay, something's funky
Jacob S.
keridee at jayco.net
Sat Mar 19 20:48:56 CET 2005
Oh thanks!!
Sometimes I just look blind...
Jacob
> Jacob S. wrote:
>> Hi everyone. Very simple set up for the problem, strange problem.
>> class Deck:
>> def __init__(self):
>> self.cards = []
>> for suit in range(4):
>> for rank in range(1,14):
>> self.cards.append(Card(suit,rank))
>> def __str__(self):
>> for card in self.cards:
>> print card
>
> __str__() should *return* a string representation of the object, not print
> it! Your __str__() method prints the Deck and returns None, which is the
> source of the error. Try
> def __str__(self):
> return '\n'.join(str(card) for card in self.cards)
>
> Kent
>
>>
>> ### End of Cards.py ###
>>
>> Nothing wrong, right?
>> So I fire up the interpreter and enter these commands
>>
>> Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on
>> win32
>> Type "help", "copyright", "credits" or "license" for more information.
>>
>>>>> from Cards import *
>>>>> a = Deck()
>>>>> print a
>>
>> Ace of Clubs
>> 2 of Clubs
>> 3 of Clubs
>> 4 of Clubs
>> 5 of Clubs
>> 6 of Clubs
>> 7 of Clubs
>> 8 of Clubs
>> 9 of Clubs
>> 10 of Clubs
>> Jack of Clubs
>> Queen of Clubs
>> King of Clubs
>> Ace of Diamonds
>> 2 of Diamonds
>> 3 of Diamonds
>> 4 of Diamonds
>> 5 of Diamonds
>> 6 of Diamonds
>> 7 of Diamonds
>> 8 of Diamonds
>> 9 of Diamonds
>> 10 of Diamonds
>> Jack of Diamonds
>> Queen of Diamonds
>> King of Diamonds
>> Ace of Hearts
>> 2 of Hearts
>> 3 of Hearts
>> 4 of Hearts
>> 5 of Hearts
>> 6 of Hearts
>> 7 of Hearts
>> 8 of Hearts
>> 9 of Hearts
>> 10 of Hearts
>> Jack of Hearts
>> Queen of Hearts
>> King of Hearts
>> Ace of Spades
>> 2 of Spades
>> 3 of Spades
>> 4 of Spades
>> 5 of Spades
>> 6 of Spades
>> 7 of Spades
>> 8 of Spades
>> 9 of Spades
>> 10 of Spades
>> Jack of Spades
>> Queen of Spades
>> King of Spades
>> Traceback (most recent call last):
>> File "<stdin>", line 1, in ?
>> TypeError: __str__ returned non-string (type NoneType)
>>
>>>>> Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on
>>>>> win32
>>
>> Type "help", "copyright", "credits" or "license" for more information.
>>
>>>>> from Cards import *
>>>>> a = Deck()
>>>>> print a
>>
>> Ace of Clubs
>> 2 of Clubs
>> 3 of Clubs
>> 4 of Clubs
>> 5 of Clubs
>> 6 of Clubs
>> 7 of Clubs
>> 8 of Clubs
>> 9 of Clubs
>> 10 of Clubs
>> Jack of Clubs
>> Queen of Clubs
>> King of Clubs
>> Ace of Diamonds
>> 2 of Diamonds
>> 3 of Diamonds
>> 4 of Diamonds
>> 5 of Diamonds
>> 6 of Diamonds
>> 7 of Diamonds
>> 8 of Diamonds
>> 9 of Diamonds
>> 10 of Diamonds
>> Jack of Diamonds
>> Queen of Diamonds
>> King of Diamonds
>> Ace of Hearts
>> 2 of Hearts
>> 3 of Hearts
>> 4 of Hearts
>> 5 of Hearts
>> 6 of Hearts
>> 7 of Hearts
>> 8 of Hearts
>> 9 of Hearts
>> 10 of Hearts
>> Jack of Hearts
>> Queen of Hearts
>> King of Hearts
>> Ace of Spades
>> 2 of Spades
>> 3 of Spades
>> 4 of Spades
>> 5 of Spades
>> 6 of Spades
>> 7 of Spades
>> 8 of Spades
>> 9 of Spades
>> 10 of Spades
>> Jack of Spades
>> Queen of Spades
>> King of Spades
>> Traceback (most recent call last):
>> File "<stdin>", line 1, in ?
>> TypeError: __str__ returned non-string (type NoneType)
>>
>>>>> Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on
>>>>> win32
>>
>> Type "help", "copyright", "credits" or "license" for more information.
>>
>>>>> from Cards import *
>>>>> a = Deck()
>>>>> print a
>>
>> Ace of Clubs
>> 2 of Clubs
>> 3 of Clubs
>> 4 of Clubs
>> 5 of Clubs
>> 6 of Clubs
>> 7 of Clubs
>> 8 of Clubs
>> 9 of Clubs
>> 10 of Clubs
>> Jack of Clubs
>> Queen of Clubs
>> King of Clubs
>> Ace of Diamonds
>> 2 of Diamonds
>> 3 of Diamonds
>> 4 of Diamonds
>> 5 of Diamonds
>> 6 of Diamonds
>> 7 of Diamonds
>> 8 of Diamonds
>> 9 of Diamonds
>> 10 of Diamonds
>> Jack of Diamonds
>> Queen of Diamonds
>> King of Diamonds
>> Ace of Hearts
>> 2 of Hearts
>> 3 of Hearts
>> 4 of Hearts
>> 5 of Hearts
>> 6 of Hearts
>> 7 of Hearts
>> 8 of Hearts
>> 9 of Hearts
>> 10 of Hearts
>> Jack of Hearts
>> Queen of Hearts
>> King of Hearts
>> Ace of Spades
>> 2 of Spades
>> 3 of Spades
>> 4 of Spades
>> 5 of Spades
>> 6 of Spades
>> 7 of Spades
>> 8 of Spades
>> 9 of Spades
>> 10 of Spades
>> Jack of Spades
>> Queen of Spades
>> King of Spades
>> Traceback (most recent call last):
>> File "<stdin>", line 1, in ?
>> TypeError: __str__ returned non-string (type NoneType)
>>
>>>>> Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on
>>>>> win32
>>
>> Type "help", "copyright", "credits" or "license" for more information.
>>
>>>>> from Cards import *
>>>>> a = Deck()
>>>>> print a
>>
>> Ace of Clubs
>> 2 of Clubs
>> 3 of Clubs
>> 4 of Clubs
>> 5 of Clubs
>> 6 of Clubs
>> 7 of Clubs
>> 8 of Clubs
>> 9 of Clubs
>> 10 of Clubs
>> Jack of Clubs
>> Queen of Clubs
>> King of Clubs
>> Ace of Diamonds
>> 2 of Diamonds
>> 3 of Diamonds
>> 4 of Diamonds
>> 5 of Diamonds
>> 6 of Diamonds
>> 7 of Diamonds
>> 8 of Diamonds
>> 9 of Diamonds
>> 10 of Diamonds
>> Jack of Diamonds
>> Queen of Diamonds
>> King of Diamonds
>> Ace of Hearts
>> 2 of Hearts
>> 3 of Hearts
>> 4 of Hearts
>> 5 of Hearts
>> 6 of Hearts
>> 7 of Hearts
>> 8 of Hearts
>> 9 of Hearts
>> 10 of Hearts
>> Jack of Hearts
>> Queen of Hearts
>> King of Hearts
>> Ace of Spades
>> 2 of Spades
>> 3 of Spades
>> 4 of Spades
>> 5 of Spades
>> 6 of Spades
>> 7 of Spades
>> 8 of Spades
>> 9 of Spades
>> 10 of Spades
>> Jack of Spades
>> Queen of Spades
>> King of Spades
>> Traceback (most recent call last):
>> File "<stdin>", line 1, in ?
>> TypeError: __str__ returned non-string (type NoneType)
>>
>>>>> a.cards
>>
>> [Ace of Clubs, 2 of Clubs, 3 of Clubs, 4 of Clubs, 5 of Clubs, 6 of
>> Clubs, 7 of
>> Clubs, 8 of Clubs, 9 of Clubs, 10 of Clubs, Jack of Clubs, Queen of
>> Clubs, King
>> of Clubs, Ace of Diamonds, 2 of Diamonds, 3 of Diamonds, 4 of Diamonds, 5
>> of Dia
>> monds, 6 of Diamonds, 7 of Diamonds, 8 of Diamonds, 9 of Diamonds, 10 of
>> Diamond
>> s, Jack of Diamonds, Queen of Diamonds, King of Diamonds, Ace of Hearts,
>> 2 of He
>> arts, 3 of Hearts, 4 of Hearts, 5 of Hearts, 6 of Hearts, 7 of Hearts, 8
>> of Hear
>> ts, 9 of Hearts, 10 of Hearts, Jack of Hearts, Queen of Hearts, King of
>> Hearts,
>> Ace of Spades, 2 of Spades, 3 of Spades, 4 of Spades, 5 of Spades, 6 of
>> Spades,
>> 7 of Spades, 8 of Spades, 9 of Spades, 10 of Spades, Jack of Spades,
>> Queen of Sp
>> ades, King of Spades]
>>
>>>>> for card in a.cards:
>>
>> ... print card
>> ...
>> Ace of Clubs
>> 2 of Clubs
>> 3 of Clubs
>> 4 of Clubs
>> 5 of Clubs
>> 6 of Clubs
>> 7 of Clubs
>> 8 of Clubs
>> 9 of Clubs
>> 10 of Clubs
>> Jack of Clubs
>> Queen of Clubs
>> King of Clubs
>> Ace of Diamonds
>> 2 of Diamonds
>> 3 of Diamonds
>> 4 of Diamonds
>> 5 of Diamonds
>> 6 of Diamonds
>> 7 of Diamonds
>> 8 of Diamonds
>> 9 of Diamonds
>> 10 of Diamonds
>> Jack of Diamonds
>> Queen of Diamonds
>> King of Diamonds
>> Ace of Hearts
>> 2 of Hearts
>> 3 of Hearts
>> 4 of Hearts
>> 5 of Hearts
>> 6 of Hearts
>> 7 of Hearts
>> 8 of Hearts
>> 9 of Hearts
>> 10 of Hearts
>> Jack of Hearts
>> Queen of Hearts
>> King of Hearts
>> Ace of Spades
>> 2 of Spades
>> 3 of Spades
>> 4 of Spades
>> 5 of Spades
>> 6 of Spades
>> 7 of Spades
>> 8 of Spades
>> 9 of Spades
>> 10 of Spades
>> Jack of Spades
>> Queen of Spades
>> King of Spades
>>
>>>>> len(a.cards)
>>
>> 52
>>
>>>>>
>>
>> Why am I getting a NoneType in my loop?
>>
>> Any help appreciated.
>> Jacob
>> _______________________________________________
>> Tutor maillist - Tutor at python.org
>> http://mail.python.org/mailman/listinfo/tutor
>>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
More information about the Tutor
mailing list