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



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

Checking a Guess


The next step is to check and see if the user’s input was a correct guess. We won’t alter any letters just yet, as we first want to make sure we can identify a correct guess and either output that they guessed correctly or remove a life:

24| game_over = True ◻◻◻
25| elif ans in word: # check if letter in word
26| print("You guessed correctly!")
27| else: # otherwise lose life
28| lives -= 1
29| print("Incorrect, you lost a life.")

Go ahead and run the cell. If you continue to guess incorrectly, you’ll notice the lives will go below zero. Be sure to guess a correct letter and incorrect letter to know that this works.

Clearing Output


Now that we’re getting further with our program, we can see that the loop is continually outputting information below previous outputs. Let’s begin to clear the output:

20| ans = input( ◻◻◻
22| clear_output( ) # clear all previous output
24| if ans == 'quit': ◻◻◻

Go ahead and run the cell. You’ll notice that it properly clears the previous information displayed no matter how long we play. This is a Jupyter Notebook specific function.

Creating the Losing Condition


The next logical operation would be creating a way to lose, since our lives can go below zero:

31| print('Incorrect, ◻◻◻ 33| if lives <= 0:
34| print("You lost all your lives, you lost!")
35| game_over = True

Go ahead and run the cell. Now if you lose all your lives, the game will stop running and tell you that you lost. Remember that the loop is only running until game_over is True, which is what we set it to once the variable of lives drops to zero.

Handling Correct Guesses


Now that we can lose, we need the ability to see correct guesses. To understand how to alter the letters shown, we first need to remember what’s being output. Our guesses list is being turned into a string and output. This means that when the user has guessed correctly, we need to change the items within our guesses list in their corresponding positions. The list is the same length of the word we chose at the beginning of the cell, so each underscore represents a letters position. If the word was “sport”, then the first underscore in “_ _ _ _” would represent the “s”. We simply need to replace the proper underscore in the list with the letter guessed. We can do this by using a for loop and keeping track of the index:

28| print('You guessed correctly!') ◻◻◻
30| # create a loop to change underscore to proper letter 31| for i in range( len(word) ):
32| i f word[ i ] == ans: # comapares values at indexes
33| guesses[ i ] = ans
34| else: ◻◻◻

ChApter 4 LIsts And Loops
Go ahead and run that cell. Now when guessing a correct letter, it will output the change. The for loop is looping to the length of the word, and we’re using the variable “i” to keep track of the index. We then check if each character is equal to the letter guessed.
If it is, then we change the item from an underscore to the letter guessed at that index. Check out Table 4-5 for an example on the process. Let’s use “pop” for our word and “p” for the guess.
Table 4-5. Tacking index value to check guess


Download 2,61 Mb.

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