Python Forum
Calling methods and getting results out
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calling methods and getting results out
#1
Hello again,
I know what I want. I know the algorithm. But I got confused writing my intention in python :(
There are cards, values, colours, complete decks, working print-method - all fine. Also, I know how to shuffle (tested it with a len(10)-list in another playground)

But I got aware, that I dont really understand whats going on in classes, methods and return values. I fumbled a little bit, tried this and that but found no solution. It would be great, if somebody can explain it to me (or showing a link to a site where it's explained) where exactly I got lost...

I tried to copy the output in the comments (line 49-52), hope that is ok? The method seems to be fine, producing no errors (until I try to print the "new" Deck2
There's a text, too. Are these sentences correct and sensible or confused blahblah?




Sincerely & thanks in advance,
HrAyas

''' Creates a Class named "Card" with two attributes:
colour: Kreuz(3), Pik(2), Herz(1), Karo(0)
value: König 13, Dame 12, Bube 11, 10, 9, ..., 2, As (1)

'''
class Card:
    #1st attributs
    colourList = ["Karo", "Herz", "Pik", "Kreuz"]
    valuelist = ["nul", "As", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Bube", "Dame", "König"]
    
    #2nd methods
    
    def __init__(self, colour = 0, value = 1):
        self.colour = colour
        self.value = value

    def __str__(self):
        return f'{self.colourList[self.colour]} {self.valuelist[self.value]}'
    
    
    
'''Now a complete set of 52cards with own methods'''
class Deck:
    '''create with a nested for-loop 4*13 = 52 cards'''
    def __init__(self):
        self.cards = []                                 # empty list---
        for farbe in range(4):
            for rang in range(1,14):
                self.cards.append(Card(farbe, rang))    # --- gets filled
    '''print with a little layout for better overview'''
    def __str__(self):
        s = ""                                          # empty string ---
        for i in range(len(self.cards)):
            s += " "*(i%13) + str(self.cards[i]) + "\n" # --- gets concatenated
        return s
    ''' What about playing with a new deck? SHUFFLE''' 
    def shuffle(self):
        import random
        '''empty list, gets filled with random items from the Deck'''
        mixed = []
        '''append a random item of 'cards' to 'mixed', len()-times'''
        for i in range(len(self.cards)):
            mixed.append(self.cards.pop(random.randint(0,len(self.cards)-1)))
        return mixed

Deck1 = Deck()

#print(Deck1)                                             # works great!
Deck2 = Deck.shuffle(Deck1)     #Create a Deck2, which is the shuffled Deck1... it's doing sth - but what?
'''Sensible and correct?
In the class "Deck" there is a method called "shuffle". 
I call it with the argument "Deck1" 
It is supposed to give me "mixed" (a list?) back. 
I call this returned thing "Deck2". '''

print(Deck2)                    # Result: 52 times <__main__.Card object at 0x0000022769A8BBC0> with different memory locations
Deck2 = Deck1.shuffle           # No error, until "print(Deck2)" is active:
print(Deck2)                    # <bound method Deck.shuffle of <__main__.Deck object at 0x000001C990A8B200>>
Reply


Messages In This Thread
Calling methods and getting results out - by HerrAyas - Apr-02-2024, 02:25 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Search Results Web results Printing the number of days in a given month and year afefDXCTN 1 3,084 Aug-21-2020, 12:20 PM
Last Post: DeaD_EyE
  How to append one function1 results to function2 results SriRajesh 5 4,575 Jan-02-2020, 12:11 PM
Last Post: Killertjuh
  a class calling one of its own methods? Skaperen 1 2,509 Jul-21-2019, 04:43 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020