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



Download 2,61 Mb.
bet72/200
Sana20.06.2022
Hajmi2,61 Mb.
#681748
1   ...   68   69   70   71   72   73   74   75   ...   200
Bog'liq
Python Projects for Beginners A Ten Week Bootcamp Approach to Python

Adding Imports


We’ll be writing this program in one cell, and it will be around 50 lines long. The first step is to import a few additional functions that we need:

1| # import additional functions
2| from random import choice
3| from IPython.display import clear_output

The second line is importing a function called “choice” which will select a random item from a list. We’ll use this to randomize the word chosen. The third line is importing a Jupyter Notebook specific function which clears the output. When using a loop, if we don’t clear output, it will continue to output on top of each other.

Declaring Game Variables


The next step is to understand what variables we need to run the game and declare them. If you think about Hangman and what we need to keep track of, we need to track the user’s lives, the word they are trying to guess, a list of words to choose from, and whether the game is over:

5| # declare game variables
6| words = [ "tree", "basket", "chair", "paper", "python" ]
7| word = choice(words) # randomly chooses a word from words list
8| guessed, lives, game_over = [ ], 7, False # multi variable assignment

Line seven declares a variable called word, which will select a random item from our words list. The eighth line is where we declare three variables together; guessed will be given the value of an empty list, lives will be set to 7, and game_over will be declared to False.
Note As we code along, feel free to write print statements to check the value of each variable. It helps to see what we’re declaring.

Generating the Hidden Word


During the game, we want the user to be able to see how many letters are within the word. To do this, we can create a list of strings, where each string is an underscore. The number of items in the list will be set to the same length of the word chosen:

10| # create a list of underscores to the length of the word
11| guesses = [ "_ " ] * len(word)

On line 11 we’re declaring a variable called guesses, which is set to a list of underscores. We get the proper length by multiplying the list by the length of the word.

Download 2,61 Mb.

Do'stlaringiz bilan baham:
1   ...   68   69   70   71   72   73   74   75   ...   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