hey guys
in the past week , ive been learning how to code for the first time and i chose python as my first programing language
today i watched a video about OOP and it got me a lil bit confused since there is a lot of “self” statement i didnt clearly understand the point of
so what is a better way to understand something other than doing a little project
i found the card game idea intersting and this is the code i wrote
import random
class Card:
def __init__(self, deck1):
self.deck=deck1
self.card=''
def deal(self):
self.card=self.deck[0]
self.deck.remove(self.card)
return self.card
class Deckofcard:
def __init__(self, deck):
self.deck=deck
def shuffle(self):
shuffleddeck=[]
if len(self.deck)==52:
for cardnumber in range(52):
rcp=random.randint(0,len(self.deck))
shuffleddeck.append(self.deck[rcp-1])
self.deck.pop(rcp-1)
return shuffleddeck
else:
return None
#deck creator
deck= []
suits = ['Hearts', 'Diamonds', 'Clubs', 'Spades']
values = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']
for suit in suits:
for value in values:
deck.append(value + ' of ' + suit)
d=Deckofcard(deck)
d.shuffle()
c=Card(deck)
print(c.deal())
the code run with no problems but i m still kinda confused
my question is ’ are my “self.” uses correct?’
also if there is bad practices i m using tell me
i m still new to the world of coding and i m enjonying it to the fullest
i can use anyhelp i get from you guys
thank you