Python Projects for Beginners a ten-Week Bootcamp Approach to Python Programming



Download 2,61 Mb.
bet121/200
Sana20.06.2022
Hajmi2,61 Mb.
#681748
1   ...   117   118   119   120   121   122   123   124   ...   200
Bog'liq
Python Projects for Beginners A Ten Week Bootcamp Approach to Python

Setting Up Imports


Let’s start writing this program by importing the necessary functions we’ll be using:

1| # importing necessary functions
2| from random import randint # allows us to get a random number
3| from IPython.display import clear_output

Feel free to test out the randint function. It takes in two arguments, a min and max, and will return a random number between those arguments. The other import we need is the ability to clear the output from the notebook cell.

Creating the Game Class


Next, we’ll begin to write our main game class, which we’ll call Blackjack. Looking at our design we created before, we’ll need to initialize the class with the attributes deck, suits, and values:

5| # create the blackjack class, which will hold all game methods and attributes 6| class Blackjack( ):
7| def __init__(self):
8| self.deck = [ ] # set to an empty list
9| self.suits = ("Spades", "Hearts", "Diamonds", "Clubs")
10| self.values = (2, 3, 4, 5, 6, 7, 8, 9, 10, "J", "Q", "K", "A")

We set the deck attribute to an empty list because we’re going to create a method that creates the deck for us. The other two attributes are created as tuples so that we can iterate over them without changing the items. We’ll use them in order to make the cards for our deck.

Generating the Deck


Using suits and values defined within the Blackjack class, we’re going to build our deck:

12| # create a method that creates a deck of 52 cards, each card should be a tuple with a value and suit
13| def makeDeck(self):
14| for suit in self.suits:
15| for value in self.values:
16| self.deck.append( (value, suit) ) # ex: (7, "Hearts")
18| game = Blackjack( )
19| game.makeDeck( )
20| print(game.deck) # remove this line after it prints out correctly

Go ahead and run the cell. Our makeDeck method has generated a full deck of 52 tuples, each with a value in the 0 index and a suit in the 1 index. We’re storing each card as a tuple because we don’t want to alter the value accidentally. In the last three lines, we create an instance of the game, call the makeDeck method, and output the value of the deck attribute. Be sure to remove the last line when you’re done, as the print statement is only being used for debugging purposes.

Download 2,61 Mb.

Do'stlaringiz bilan baham:
1   ...   117   118   119   120   121   122   123   124   ...   200




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish