Results 1 to 5 of 5

Thread: Python: Searching for contents in __str__

  1. #1
    Join Date
    Dec 2008
    Beans
    57
    Distro
    Xubuntu 8.04 Hardy Heron

    Python: Searching for contents in __str__

    I am trying to create a specialist dominos programme, where the __str__ method of each hand is populated by dominos such as "1|6\t3|6\t6|6"

    To start I need to find the hand with "6|6" and wrote the following code (extract):

    Code:
    while not starter:
            for i in range(6, -1, -1):
                double = str(i) + "|" + str(i)
                for player in self.players:
                    if double in player:
                        starter = True
    the code "print(player)" prints the string correctly, but the line "if double in player" returns the error "TypeError: argument of type 'Player_Hand' is not iterable"

    What am I doing wrong? Sorry for the noob question...
    Last edited by izziere; May 27th, 2013 at 12:35 PM.
    1998 Compaq Deskpro | Pentium II 350 Mhz CPU | 256 Mb RAM | 6.1Gb 33Mhz ATA Hard Drive | No sound card

  2. #2
    Join Date
    May 2008
    Location
    UK
    Beans
    1,448
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: Python: Searching for contents in __str__

    Is there a reason you are storing the hand in a single string ? A much better way (I would have thought) would be to have a list of the dominoes.

    Even though the _str_ attribute of your Player_Hand returns a string, when you write a for loop on a custom type, it doesn't use the str attribute to create that loop. It looks at the whole type definition and looks for the appropriate iteration methods on the type :

    http://docs.python.org/release/2.6.8...html#iterators

    the other way of doing this - if you insist on using a string representation, is to do :
    Code:
    if double in str(player):
    But learning iterators and using the power of python would be much better IMHO.
    Last edited by Tony Flury; May 26th, 2013 at 11:15 PM.
    Tony - Happy to try to help.
    Unless otherwise stated - all code posted by me is untested. Remember to Mark the Thread as Solved.
    Ubuntu user number # 24044 Projects : TimeWarp - on the fly Backups

  3. #3
    Join Date
    Jun 2009
    Location
    Land of Paranoia and Guns
    Beans
    194
    Distro
    Ubuntu 12.10 Quantal Quetzal

    Re: Python: Searching for contents in __str__

    Try this:
    Code:
    if double in str(player):
    or, you could just use __str__ directly:
    Code:
    if double in player.__str__():
    Both methods work, but I belive the former is the conventional method. You could also be wild and implement an iterator in your Player_Hand class:
    Code:
    class Player_Hand():
            ...
            def __iter__(self): return iter(str(self))
    Don't use W3Schools as a resource! (Inconsequential foul language at the jump)
    Open Linux Forums (More foul language, but well worth it for the quality of support and good humor.)
    If you want to discuss W3Schools, please PM me instead of posting.

  4. #4
    Join Date
    Dec 2008
    Beans
    57
    Distro
    Xubuntu 8.04 Hardy Heron

    Re: Python: Searching for contents in __str__

    Thanks both - that's really helpful. I was using the string because I am ripping off the Blackjack programme in Python Programming for the Absolute Beginner and the string is the method used to store the cards in the hand in that example. Essentially I am trying to learn by making mistakes. The good news is that I am clearly learning a lot, judging by the number of mistakes!
    1998 Compaq Deskpro | Pentium II 350 Mhz CPU | 256 Mb RAM | 6.1Gb 33Mhz ATA Hard Drive | No sound card

  5. #5
    Join Date
    Apr 2013
    Location
    43.49°N 7.46°E
    Beans
    117
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Python: Searching for contents in __str__

    Quote Originally Posted by izziere View Post
    Essentially I am trying to learn by making mistakes. The good news is that I am clearly learning a lot, judging by the number of mistakes!
    Solving mistakes is the best way for gaining experience (in programming or even about the life).
    Regarding to your first loop, it could be rewritten as follows
    Code:
    for i in range(7):
        double = "|".join([str(i),str(i)])
    by using the join method.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •