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



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

Creating the Game Loop


Every game has a main loop no matter the size of the program. Our main loop will perform the logic that we defined in our Final Design section. Rather than writing it all out at once, let’s take small steps. The first step is to be able to accept user input and stop playing the game:

13| # create main game loop 14| while not game_over:
15| ans = input("Type quit or guess a letter: ").lower( ) 17| if ans == "quit":
18| print("Thanks for playing.")
19| game_over = True

Go ahead and run the cell. If you type “quit”, the program should stop as we are looping until game_over is set to True, which only occurs when we input “quit”.
Note Always make sure the cell is done running before moving on.

Outputting Game Information


The next step is to start outputting information to the user. Let’s output their lives and the word that they’re trying to guess in a nicely formatted statement:

14| while not game_over: ◻◻◻
15| # output game information
16| hidden_word = "".join(guesses)
17| print( "Word to guess: { }".format(hidden_word) )
18| print( "Lives: { }".format(lives) )
20| ans = input( ◻◻◻

Go ahead and run the cell. Depending on the word chosen, you’ll get a different output. If the word chosen was four letters, we’ll get an output of “Word to guess: _ _ _ _” and “Lives: 7”. The format is nothing new, but what about line 16? The reason we’re able to create a string of underscores to output in line 17 is because of the join method. It states that we want to join all the items within the guesses list together with no spaces in between. For example:
>>> chars = ['h', 'e', 'l', 'l', 'o']
>>> print('-'.join(chars))
The preceding two lines would output “h-e-l-l-o”. This is a simple way to display our list as a string.

Download 2,61 Mb.

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