国产亚洲精品福利在线无卡一,国产精久久一区二区三区,亚洲精品无码国模,精品久久久久久无码专区不卡

當(dāng)前位置: 首頁(yè) > news >正文

政府網(wǎng)站建設(shè)事例常見(jiàn)的推廣方式有哪些

政府網(wǎng)站建設(shè)事例,常見(jiàn)的推廣方式有哪些,做網(wǎng)站公司的介紹,想查客戶信息做網(wǎng)站三十一、猜數(shù)字 原文:http://inventwithpython.com/bigbookpython/project31.html 猜數(shù)字是初學(xué)者練習(xí)基本編程技術(shù)的經(jīng)典游戲。在這個(gè)游戲中,電腦會(huì)想到一個(gè)介于 1 到 100 之間的隨機(jī)數(shù)。玩家有 10 次機(jī)會(huì)猜出數(shù)字。每次猜中后,電腦會(huì)告訴玩…

三十一、猜數(shù)字

原文:http://inventwithpython.com/bigbookpython/project31.html

猜數(shù)字是初學(xué)者練習(xí)基本編程技術(shù)的經(jīng)典游戲。在這個(gè)游戲中,電腦會(huì)想到一個(gè)介于 1 到 100 之間的隨機(jī)數(shù)。玩家有 10 次機(jī)會(huì)猜出數(shù)字。每次猜中后,電腦會(huì)告訴玩家它是太高還是太低。

運(yùn)行示例

當(dāng)您運(yùn)行guess.py時(shí),輸出將如下所示:

Guess the Number, by Al Sweigart email@protectedI am thinking of a number between 1 and 100.
You have 10 guesses left. Take a guess.
> 50
Your guess is too high.
You have 9 guesses left. Take a guess.
> 25
Your guess is too low.
`--snip--`
You have 5 guesses left. Take a guess.
> 42
Yay! You guessed my number!

工作原理

猜數(shù)字使用了幾個(gè)基本的編程概念:循環(huán)、if-else語(yǔ)句、函數(shù)、方法調(diào)用和隨機(jī)數(shù)。Python 的random模塊生成偽隨機(jī)數(shù)——看似隨機(jī)但技術(shù)上可預(yù)測(cè)的數(shù)字。對(duì)于計(jì)算機(jī)來(lái)說(shuō),偽隨機(jī)數(shù)比真正的隨機(jī)數(shù)更容易生成,對(duì)于視頻游戲和一些科學(xué)模擬等應(yīng)用來(lái)說(shuō),偽隨機(jī)數(shù)被認(rèn)為是“足夠隨機(jī)”的。

Python 的random模塊從一個(gè)種子值產(chǎn)生偽隨機(jī)數(shù),從同一個(gè)種子產(chǎn)生的每個(gè)偽隨機(jī)數(shù)流都將是相同的。例如,在交互式 shell 中輸入以下內(nèi)容:

>>> import random
>>> random.seed(42)
>>> random.randint(1, 10); random.randint(1, 10); random.randint(1, 10)
2
1
5

如果您重新啟動(dòng)交互式 shell 并再次運(yùn)行這段代碼,它會(huì)產(chǎn)生相同的偽隨機(jī)數(shù):2、15。視頻游戲《我的世界》(也叫《挖礦爭(zhēng)霸》)從起始種子值生成其偽隨機(jī)虛擬世界,這就是為什么不同的玩家可以通過(guò)使用相同的種子來(lái)重新創(chuàng)建相同的世界。

"""Guess the Number, by Al Sweigart email@protected
Try to guess the secret number based on hints.
This code is available at https://nostarch.com/big-book-small-python-programming
Tags: tiny, beginner, game"""import randomdef askForGuess():while True:guess = input('> ')  # Enter the guess.if guess.isdecimal():return int(guess)  # Convert string guess to an integer.print('Please enter a number between 1 and 100.')print('Guess the Number, by Al Sweigart email@protected')
print()
secretNumber = random.randint(1, 100)  # Select a random number.
print('I am thinking of a number between 1 and 100.')for i in range(10):  # Give the player 10 guesses.print('You have {} guesses left. Take a guess.'.format(10 - i))guess = askForGuess()if guess == secretNumber:break  # Break out of the for loop if the guess is correct.# Offer a hint:if guess < secretNumber:print('Your guess is too low.')if guess > secretNumber:print('Your guess is too high.')# Reveal the results:
if guess == secretNumber:print('Yay! You guessed my number!')
else:print('Game over. The number I was thinking of was', secretNumber) 

在輸入源代碼并運(yùn)行幾次之后,嘗試對(duì)其進(jìn)行實(shí)驗(yàn)性的修改。你也可以自己想辦法做到以下幾點(diǎn):

  • 創(chuàng)建一個(gè)“猜字母”變體,根據(jù)玩家猜測(cè)的字母順序給出提示。
  • 根據(jù)玩家之前的猜測(cè),在每次猜測(cè)后提示說(shuō)“更熱”或“更冷”。

探索程序

試著找出下列問(wèn)題的答案。嘗試對(duì)代碼進(jìn)行一些修改,然后重新運(yùn)行程序,看看這些修改有什么影響。

  1. 如果把第 11 行的input('> ')改成input(secretNumber)會(huì)怎么樣?
  2. 如果將第 14 行的return int(guess)改為return guess,會(huì)得到什么錯(cuò)誤信息?
  3. 如果把第 20 行的random.randint(1, 100)改成random.randint(1, 1)會(huì)怎么樣?
  4. 如果把第 24 行的format(10 - i)改成format(i)會(huì)怎么樣?
  5. 如果將第 37 行的guess == secretNumber改為guess = secretNumber,會(huì)得到什么錯(cuò)誤信息?

三十二、容易受騙的人

原文:http://inventwithpython.com/bigbookpython/project32.html

在這個(gè)簡(jiǎn)短的節(jié)目中,你可以學(xué)到讓一個(gè)容易受騙的人忙碌幾個(gè)小時(shí)的秘密和微妙的藝術(shù)。我不會(huì)破壞這里的妙語(yǔ)。復(fù)制代碼并自己運(yùn)行。這個(gè)項(xiàng)目對(duì)初學(xué)者來(lái)說(shuō)很棒,不管你是聰明的還是。。。不太聰明。

運(yùn)行示例

當(dāng)您運(yùn)行gullible.py時(shí),輸出將如下所示:

Gullible, by Al Sweigart email@protected
Do you want to know how to keep a gullible person busy for hours? Y/N
> y
Do you want to know how to keep a gullible person busy for hours? Y/N
> y
Do you want to know how to keep a gullible person busy for hours? Y/N
> yes
Do you want to know how to keep a gullible person busy for hours? Y/N
> YES
Do you want to know how to keep a gullible person busy for hours? Y/N
> TELL ME HOW TO KEEP A GULLIBLE PERSON BUSY FOR HOURS
"TELL ME HOW TO KEEP A GULLIBLE PERSON BUSY FOR HOURS" is not a valid yes/no response.
Do you want to know how to keep a gullible person busy for hours? Y/N
> y
Do you want to know how to keep a gullible person busy for hours? Y/N
> y
Do you want to know how to keep a gullible person busy for hours? Y/N
> n
Thank you. Have a nice day!

工作原理

為了更加用戶友好,你的程序應(yīng)該嘗試解釋用戶可能的輸入。例如,這個(gè)程序問(wèn)用戶一個(gè)是/否的問(wèn)題,但是對(duì)于玩家來(lái)說(shuō),簡(jiǎn)單地輸入yn而不是輸入完整的單詞會(huì)更簡(jiǎn)單。如果玩家的CapsLock鍵被激活,程序也可以理解玩家的意圖,因?yàn)樗鼤?huì)在玩家輸入的字符串上調(diào)用lower()字符串方法。這樣,'y'、'yes'、'Y'、'Yes'、'YES'都被程序解釋的一樣。玩家的負(fù)面反應(yīng)也是如此。

"""Gullible, by Al Sweigart email@protected
How to keep a gullible person busy for hours. (This is a joke program.)
This code is available at https://nostarch.com/big-book-small-python-programming
Tags: tiny, beginner, humor"""print('Gullible, by Al Sweigart email@protected')while True:  # Main program loop.print('Do you want to know how to keep a gullible person busy for hours? Y/N')response = input('> ')  # Get the user's response.if response.lower() == 'no' or response.lower() == 'n':break  # If "no", break out of this loop.if response.lower() == 'yes' or response.lower() == 'y':continue  # If "yes", continue to the start of this loop.print('"{}" is not a valid yes/no response.'.format(response))print('Thank you. Have a nice day!') 

探索程序

試著找出下列問(wèn)題的答案。嘗試對(duì)代碼進(jìn)行一些修改,然后重新運(yùn)行程序,看看這些修改有什么影響。

  1. 如果把第 11 行的response.lower() == 'no'改成response.lower() != 'no'會(huì)怎么樣?
  2. 如果把第 8 行的while True:改成while False:會(huì)怎么樣?

三十三、黑客小游戲

原文:http://inventwithpython.com/bigbookpython/project33.html

在這個(gè)游戲中,玩家必須通過(guò)猜測(cè)一個(gè)七個(gè)字母的單詞作為秘密密碼來(lái)入侵電腦。電腦的記憶庫(kù)顯示可能的單詞,并提示玩家每次猜測(cè)的接近程度。例如,如果密碼是MONITOR,但玩家猜了CONTAIN,他們會(huì)得到提示,七個(gè)字母中有兩個(gè)是正確的,因?yàn)?code>MONITOR和CONTAIN的第二個(gè)和第三個(gè)字母都是字母ON。這個(gè)游戲類(lèi)似于項(xiàng)目 1,“百吉餅”,以及輻射系列視頻游戲中的黑客迷你游戲。

運(yùn)行示例

當(dāng)您運(yùn)行hacking.py時(shí),輸出將如下所示:

Hacking Minigame, by Al Sweigart email@protected
Find the password in the computer's memory:
0x1150  $],>@|~~RESOLVE^    0x1250  {>+)<!?CHICKEN,%
0x1160  }@%_-:;/$^(|<|!(    0x1260  .][})?#@#ADDRESS
0x1170  _;)][#?<&~$~+&}}    0x1270  ,#=)>{-;/DESPITE
0x1180  %[!]{email@protected?~,    0x1280  }/.}!-DISPLAY%%/
0x1190  _[^%[@}^<_+{email@protected$~    0x1290  =>>,:*%email@protected+{%#.
0x11a0  )?~/)+PENALTY?-=    0x12a0  >[,?*#email@protected$/
`--snip--`
Enter password: (4 tries remaining)
> resolve
Access Denied (2/7 correct)
Enter password: (3 tries remaining)
> improve
A C C E S S   G R A N T E D

工作原理

這個(gè)游戲有一個(gè)黑客主題,但不涉及任何實(shí)際的電腦黑客行為。如果我們只是在屏幕上列出可能的單詞,游戲就會(huì)完全一樣。然而,模仿計(jì)算機(jī)記憶庫(kù)的裝飾性添加傳達(dá)了一種令人興奮的計(jì)算機(jī)黑客的感覺(jué)。對(duì)細(xì)節(jié)和用戶體驗(yàn)的關(guān)注將一個(gè)平淡、無(wú)聊的游戲變成了一個(gè)令人興奮的游戲。

"""Hacking Minigame, by Al Sweigart email@protected
The hacking mini-game from "Fallout 3". Find out which seven-letter
word is the password by using clues each guess gives you.
This code is available at https://nostarch.com/big-book-small-python-programming
Tags: large, artistic, game, puzzle"""# NOTE: This program requires the sevenletterwords.txt file. You can
# download it from https://inventwithpython.com/sevenletterwords.txtimport random, sys# Set up the constants:
# The garbage filler characters for the "computer memory" display.
GARBAGE_CHARS = 'email@protected#$%^&*()_+-={}[]|;:,.<>?/'# Load the WORDS list from a text file that has 7-letter words.
with open('sevenletterwords.txt') as wordListFile:WORDS = wordListFile.readlines()
for i in range(len(WORDS)):# Convert each word to uppercase and remove the trailing newline:WORDS[i] = WORDS[i].strip().upper()def main():"""Run a single game of Hacking."""print('''Hacking Minigame, by Al Sweigart email@protected
Find the password in the computer's memory. You are given clues after
each guess. For example, if the secret password is MONITOR but the
player guessed CONTAIN, they are given the hint that 2 out of 7 letters
were correct, because both MONITOR and CONTAIN have the letter O and N
as their 2nd and 3rd letter. You get four guesses.\n''')input('Press Enter to begin...')gameWords = getWords()# The "computer memory" is just cosmetic, but it looks cool:computerMemory = getComputerMemoryString(gameWords)secretPassword = random.choice(gameWords)print(computerMemory)# Start at 4 tries remaining, going down:for triesRemaining in range(4, 0, -1):playerMove = askForPlayerGuess(gameWords, triesRemaining)if playerMove == secretPassword:print('A C C E S S   G R A N T E D')returnelse:numMatches = numMatchingLetters(secretPassword, playerMove)print('Access Denied ({}/7 correct)'.format(numMatches))print('Out of tries. Secret password was {}.'.format(secretPassword))def getWords():"""Return a list of 12 words that could possibly be the password.The secret password will be the first word in the list.To make the game fair, we try to ensure that there are words witha range of matching numbers of letters as the secret word."""secretPassword = random.choice(WORDS)words = [secretPassword]# Find two more words; these have zero matching letters.# We use "< 3" because the secret password is already in words.while len(words) < 3:randomWord = getOneWordExcept(words)if numMatchingLetters(secretPassword, randomWord) == 0:words.append(randomWord)# Find two words that have 3 matching letters (but give up at 500# tries if not enough can be found).for i in range(500):if len(words) == 5:break  # Found 5 words, so break out of the loop.randomWord = getOneWordExcept(words)if numMatchingLetters(secretPassword, randomWord) == 3:words.append(randomWord)# Find at least seven words that have at least one matching letter# (but give up at 500 tries if not enough can be found).for i in range(500):if len(words) == 12:break  # Found 7 or more words, so break out of the loop.randomWord = getOneWordExcept(words)if numMatchingLetters(secretPassword, randomWord) != 0:words.append(randomWord)# Add any random words needed to get 12 words total.while len(words) < 12:randomWord = getOneWordExcept(words)words.append(randomWord)assert len(words) == 12return wordsdef getOneWordExcept(blocklist=None):"""Returns a random word from WORDS that isn't in blocklist."""if blocklist == None:blocklist = []while True:randomWord = random.choice(WORDS)if randomWord not in blocklist:return randomWorddef numMatchingLetters(word1, word2):"""Returns the number of matching letters in these two words."""matches = 0for i in range(len(word1)):if word1[i] == word2[i]:matches += 1return matchesdef getComputerMemoryString(words):"""Return a string representing the "computer memory"."""# Pick one line per word to contain a word. There are 16 lines, but# they are split into two halves.linesWithWords = random.sample(range(16 * 2), len(words))# The starting memory address (this is also cosmetic).memoryAddress = 16 * random.randint(0, 4000)# Create the "computer memory" string.computerMemory = []  # Will contain 16 strings, one for each line.nextWord = 0  # The index in words of the word to put into a line.for lineNum in range(16):  # The "computer memory" has 16 lines.# Create a half line of garbage characters:leftHalf = ''rightHalf = ''for j in range(16):  # Each half line has 16 characters.leftHalf += random.choice(GARBAGE_CHARS)rightHalf += random.choice(GARBAGE_CHARS)# Fill in the password from words:if lineNum in linesWithWords:# Find a random place in the half line to insert the word:insertionIndex = random.randint(0, 9)# Insert the word:leftHalf = (leftHalf[:insertionIndex] + words[nextWord]+ leftHalf[insertionIndex + 7:])nextWord += 1  # Update the word to put in the half line.if lineNum + 16 in linesWithWords:# Find a random place in the half line to insert the word:insertionIndex = random.randint(0, 9)# Insert the word:rightHalf = (rightHalf[:insertionIndex] + words[nextWord]+ rightHalf[insertionIndex + 7:])nextWord += 1  # Update the word to put in the half line.computerMemory.append('0x' + hex(memoryAddress)[2:].zfill(4)+ '  ' + leftHalf + '    '+ '0x' + hex(memoryAddress + (16*16))[2:].zfill(4)+ '  ' + rightHalf)memoryAddress += 16  # Jump from, say, 0xe680 to 0xe690.# Each string in the computerMemory list is joined into one large# string to return:return '\n'.join(computerMemory)def askForPlayerGuess(words, tries):"""Let the player enter a password guess."""while True:print('Enter password: ({} tries remaining)'.format(tries))guess = input('> ').upper()if guess in words:return guessprint('That is not one of the possible passwords listed above.')print('Try entering "{}" or "{}".'.format(words[0], words[1]))# If this program was run (instead of imported), run the game:
if __name__ == '__main__':try:main()except KeyboardInterrupt:sys.exit()  # When Ctrl-C is pressed, end the program. 

在輸入源代碼并運(yùn)行幾次之后,嘗試對(duì)其進(jìn)行實(shí)驗(yàn)性的修改。你也可以自己想辦法做到以下幾點(diǎn):

  • 在互聯(lián)網(wǎng)上找到一個(gè)單詞列表,創(chuàng)建你自己的文件sevenletterwords.txt,也許是一個(gè)由六個(gè)或八個(gè)字母組成的文件。
  • 創(chuàng)建一個(gè)不同的“計(jì)算機(jī)內(nèi)存”的可視化

探索程序

試著找出下列問(wèn)題的答案。嘗試對(duì)代碼進(jìn)行一些修改,然后重新運(yùn)行程序,看看這些修改有什么影響。

  1. 如果把 133 行的for j in range(16):改成for j in range(0):會(huì)怎么樣?
  2. 如果把第 14 行的GARBAGE_CHARS = 'email@protected#$%^&*()_+-={}[]|;:,.<>?/'改成GARBAGE_CHARS = '.'會(huì)怎么樣?
  3. 如果把第 34 行的gameWords = getWords()改成gameWords = ['MALKOVICH'] * 20會(huì)怎么樣?
  4. 如果將第 94 行的return words改為return,會(huì)得到什么錯(cuò)誤信息?
  5. 如果把 103 行的randomWord = random.choice(WORDS)改成secretPassword = 'PASSWORD'會(huì)怎么樣?

三十四、劊子手和斷頭臺(tái)

原文:http://inventwithpython.com/bigbookpython/project34.html

這個(gè)經(jīng)典的文字游戲讓玩家猜一個(gè)秘密單詞的字母。對(duì)于每一個(gè)不正確的字母,劊子手的另一部分被畫(huà)出來(lái)。在劊子手完成之前,試著猜出完整的單詞。這個(gè)版本的密語(yǔ)都是兔子鴿子之類(lèi)的動(dòng)物,但是你可以用自己的一套話來(lái)代替這些。

HANGMAN_PICS變量包含劊子手絞索每一步的 ASCII 藝術(shù)畫(huà)字符串:

 +--+     +--+     +--+     +--+     +--+     +--+     +--+|  |     |  |     |  |     |  |     |  |     |  |     |  ||     O  |     O  |     O  |     O  |     O  |     O  ||        |     |  |    /|  |    /|\ |    /|\ |    /|\ ||        |        |        |        |    /   |    / \ ||        |        |        |        |        |        |
=====    =====    =====    =====    =====    =====    =====

對(duì)于游戲中的法式轉(zhuǎn)折,您可以用以下描述斷頭臺(tái)的字符串替換HANGMAN_PICS變量中的字符串:

|        |   |    |===|    |===|    |===|    |===|    |===|
|        |   |    |   |    |   |    |   |    || /|    || /|
|        |   |    |   |    |   |    |   |    ||/ |    ||/ |
|        |   |    |   |    |   |    |   |    |   |    |   |
|        |   |    |   |    |   |    |   |    |   |    |   |
|        |   |    |   |    |   |    |/-\|    |/-\|    |/-\|
|        |   |    |   |    |\ /|    |\ /|    |\ /|    |\O/|
|===     |===|    |===|    |===|    |===|    |===|    |===|

運(yùn)行示例

當(dāng)您運(yùn)行hangman.py時(shí),輸出將如下所示:

Hangman, by Al Sweigart email@protected+--+|  |||||
=====
The category is: AnimalsMissed letters: No missed letters yet._ _ _ _ _
Guess a letter.
> e
`--snip--`+--+|  |O  |
/|  |||
=====
The category is: AnimalsMissed letters: A I S
O T T E _
Guess a letter.
> r
Yes! The secret word is: OTTER
You have won!

工作原理

劊子手和斷頭臺(tái)共享相同的游戲機(jī)制,但有不同的表現(xiàn)形式。這使得用 ASCII 藝術(shù)畫(huà)的斷頭臺(tái)圖形替換 ASCII 藝術(shù)畫(huà)的絞索圖形變得容易,而不必改變程序遵循的主要邏輯。程序的表示和邏輯部分的分離使得用新的特性或不同的設(shè)計(jì)進(jìn)行更新變得更加容易。在專業(yè)軟件開(kāi)發(fā)中,這種策略是軟件設(shè)計(jì)模式軟件架構(gòu)的一個(gè)例子,它關(guān)注于如何構(gòu)建你的程序,以便于理解和修改。這主要在大型軟件應(yīng)用中有用,但是您也可以將這些原則應(yīng)用到較小的項(xiàng)目中。

"""Hangman, by Al Sweigart email@protected
Guess the letters to a secret word before the hangman is drawn.
This code is available at https://nostarch.com/big-book-small-python-programming
Tags: large, game, word, puzzle"""# A version of this game is featured in the book "Invent Your Own
# Computer Games with Python" https://nostarch.com/inventwithpythonimport random, sys# Set up the constants:
# (!) Try adding or changing the strings in HANGMAN_PICS to make a
# guillotine instead of a gallows.
HANGMAN_PICS = [r"""
+--+
|  |||||
=====""",
r"""
+--+
|  |
O  ||||
=====""",
r"""
+--+
|  |
O  |
|  |||
=====""",
r"""
+--+
|  |
O  |
/|  |||
=====""",
r"""
+--+
|  |
O  |
/|\ |||
=====""",
r"""
+--+
|  |
O  |
/|\ |
/   ||
=====""",
r"""
+--+
|  |
O  |
/|\ |
/ \ ||
====="""]# (!) Try replacing CATEGORY and WORDS with new strings.
CATEGORY = 'Animals'
WORDS = 'ANT BABOON BADGER BAT BEAR BEAVER CAMEL CAT CLAM COBRA COUGAR COYOTE CROW DEER DOG DONKEY DUCK EAGLE FERRET FOX FROG GOAT GOOSE HAWK LION LIZARD LLAMA MOLE MONKEY MOOSE MOUSE MULE NEWT OTTER OWL PANDA PARROT PIGEON PYTHON RABBIT RAM RAT RAVEN RHINO SALMON SEAL SHARK SHEEP SKUNK SLOTH SNAKE SPIDER STORK SWAN TIGER TOAD TROUT TURKEY TURTLE WEASEL WHALE WOLF WOMBAT ZEBRA'.split()def main():print('Hangman, by Al Sweigart email@protected')# Setup variables for a new game:missedLetters = []  # List of incorrect letter guesses.correctLetters = []  # List of correct letter guesses.secretWord = random.choice(WORDS)  # The word the player must guess.while True:  # Main game loop.drawHangman(missedLetters, correctLetters, secretWord)# Let the player enter their letter guess:guess = getPlayerGuess(missedLetters + correctLetters)if guess in secretWord:# Add the correct guess to correctLetters:correctLetters.append(guess)# Check if the player has won:foundAllLetters = True  # Start off assuming they've won.for secretWordLetter in secretWord:if secretWordLetter not in correctLetters:# There's a letter in the secret word that isn't# yet in correctLetters, so the player hasn't won:foundAllLetters = Falsebreakif foundAllLetters:print('Yes! The secret word is:', secretWord)print('You have won!')break  # Break out of the main game loop.else:# The player has guessed incorrectly:missedLetters.append(guess)# Check if player has guessed too many times and lost. (The# "- 1" is because we don't count the empty gallows in# HANGMAN_PICS.)if len(missedLetters) == len(HANGMAN_PICS) - 1:drawHangman(missedLetters, correctLetters, secretWord)print('You have run out of guesses!')print('The word was "{}"'.format(secretWord))breakdef drawHangman(missedLetters, correctLetters, secretWord):"""Draw the current state of the hangman, along with the missed andcorrectly-guessed letters of the secret word."""print(HANGMAN_PICS[len(missedLetters)])print('The category is:', CATEGORY)print()# Show the incorrectly guessed letters:print('Missed letters: ', end='')for letter in missedLetters:print(letter, end=' ')if len(missedLetters) == 0:print('No missed letters yet.')print()# Display the blanks for the secret word (one blank per letter):blanks = ['_'] * len(secretWord)# Replace blanks with correctly guessed letters:for i in range(len(secretWord)):if secretWord[i] in correctLetters:blanks[i] = secretWord[i]# Show the secret word with spaces in between each letter:print(' '.join(blanks))def getPlayerGuess(alreadyGuessed):"""Returns the letter the player entered. This function makes surethe player entered a single letter they haven't guessed before."""while True:  # Keep asking until the player enters a valid letter.print('Guess a letter.')guess = input('> ').upper()if len(guess) != 1:print('Please enter a single letter.')elif guess in alreadyGuessed:print('You have already guessed that letter. Choose again.')elif not guess.isalpha():print('Please enter a LETTER.')else:return guess# If this program was run (instead of imported), run the game:
if __name__ == '__main__':try:main()except KeyboardInterrupt:sys.exit()  # When Ctrl-C is pressed, end the program. 

在輸入源代碼并運(yùn)行幾次之后,嘗試對(duì)其進(jìn)行實(shí)驗(yàn)性的修改。標(biāo)有(!)的注釋對(duì)你可以做的小改變有建議。你也可以自己想辦法做到以下幾點(diǎn):

  • 添加一個(gè)“類(lèi)別選擇”功能,讓玩家選擇他們想玩的詞的類(lèi)別。
  • 增加一個(gè)選擇功能,這樣玩家可以在游戲的劊子手和斷頭臺(tái)版本之間進(jìn)行選擇。

探索程序

試著找出下列問(wèn)題的答案。嘗試對(duì)代碼進(jìn)行一些修改,然后重新運(yùn)行程序,看看這些修改有什么影響。

  1. 如果刪除或注釋掉第 108 行的missedLetters.append(guess)會(huì)發(fā)生什么?
  2. 如果把第 85 行的drawHangman(missedLetters, correctLetters, secretWord)改成drawHangman(correctLetters, missedLetters, secretWord)會(huì)怎么樣?
  3. 如果把 136 行的['_']改成['*']會(huì)怎么樣?
  4. 如果把 144 行的print(' '.join(blanks))改成print(secretWord)會(huì)怎么樣?

三十五、六邊形網(wǎng)格

原文:http://inventwithpython.com/bigbookpython/project35.html

這個(gè)簡(jiǎn)短的程序產(chǎn)生一個(gè)類(lèi)似于鐵絲網(wǎng)的六角形網(wǎng)格的鑲嵌圖像。這說(shuō)明你不需要很多代碼就能做出有趣的東西。這個(gè)項(xiàng)目的一個(gè)稍微復(fù)雜一點(diǎn)的變體是項(xiàng)目 65,“閃光地毯”

注意,這個(gè)程序使用原始字符串,它在開(kāi)始的引號(hào)前面加上小寫(xiě)的r,這樣字符串中的反斜杠就不會(huì)被解釋為轉(zhuǎn)義字符。

運(yùn)行示例

圖 35-1 顯示了運(yùn)行hexgrid.py時(shí)的輸出。

f35001

:顯示六邊形網(wǎng)格鑲嵌圖像的輸出

工作原理

編程背后的力量在于它能讓計(jì)算機(jī)快速無(wú)誤地執(zhí)行重復(fù)的指令。這就是十幾行代碼如何在屏幕上創(chuàng)建數(shù)百、數(shù)千或數(shù)百萬(wàn)個(gè)六邊形。

在命令提示符或終端窗口中,您可以將程序的輸出從屏幕重定向到文本文件。在 Windows 上,運(yùn)行py hexgrid.py > hextiles.txt創(chuàng)建一個(gè)包含六邊形的文本文件。在 Linux 和 macOS 上,運(yùn)行python3 hexgrid.py > hextiles.txt。沒(méi)有屏幕大小的限制,您可以增加X_REPEATY_REPEAT常量并將內(nèi)容保存到文件中。從那里,很容易將文件打印在紙上,用電子郵件發(fā)送,或發(fā)布到社交媒體上。這適用于你創(chuàng)作的任何計(jì)算機(jī)生成的作品。

"""Hex Grid, by Al Sweigart email@protected
Displays a simple tessellation of a hexagon grid.
This code is available at https://nostarch.com/big-book-small-python-programming
Tags: tiny, beginner, artistic"""# Set up the constants:
# (!) Try changing these values to other numbers:
X_REPEAT = 19  # How many times to tessellate horizontally.
Y_REPEAT = 12  # How many times to tessellate vertically.for y in range(Y_REPEAT):# Display the top half of the hexagon:for x in range(X_REPEAT):print(r'/ \_', end='')print()# Display the bottom half of the hexagon:for x in range(X_REPEAT):print(r'\_/ ', end='')print() 

在輸入源代碼并運(yùn)行幾次之后,嘗試對(duì)其進(jìn)行實(shí)驗(yàn)性的修改。標(biāo)有(!)的注釋對(duì)你可以做的小改變有建議。你也可以自己想辦法做到以下幾點(diǎn):

  • 創(chuàng)建更大尺寸的平鋪六邊形。
  • 創(chuàng)建平鋪的矩形磚塊,而不是六邊形。

為了練習(xí),請(qǐng)嘗試使用更大的六邊形網(wǎng)格重新創(chuàng)建此程序,如下圖所示:

 /   \     /   \     /   \     /   \     /   \     /   \     /   \
/     \___/     \___/     \___/     \___/     \___/     \___/     \
\     /   \     /   \     /   \     /   \     /   \     /   \     /\___/     \___/     \___/     \___/     \___/     \___/     \___//   \     /   \     /   \     /   \     /   \     /   \     /   \
/     \___/     \___/     \___/     \___/     \___/     \___/     \/     \         /     \         /     \         /     \/       \       /       \       /       \       /       \
/         \_____/         \_____/         \_____/         \_____
\         /     \         /     \         /     \         /\       /       \       /       \       /       \       /\_____/         \_____/         \_____/         \_____//     \         /     \         /     \         /     \/       \       /       \       /       \       /       \
/         \_____/         \_____/         \_____/         \_____ 

探索程序

這是一個(gè)基礎(chǔ)程序,所以沒(méi)有太多的選項(xiàng)來(lái)定制它。取而代之的是,考慮你如何能類(lèi)似地編程其他形狀的模式。

http://m.aloenet.com.cn/news/32546.html

相關(guān)文章:

  • 遼河油田建設(shè)有限公司網(wǎng)站找個(gè)網(wǎng)站
  • 9420高清免費(fèi)視頻在線觀看武漢抖音seo搜索
  • 做網(wǎng)站需要懂什么廣州網(wǎng)頁(yè)定制多少錢(qián)
  • 做怎么樣的網(wǎng)站好如何自己弄個(gè)免費(fèi)網(wǎng)站
  • 怎么做一元購(gòu)網(wǎng)站代運(yùn)營(yíng)公司哪家好一些
  • 做網(wǎng)站靠教育賺錢(qián)seo的基礎(chǔ)優(yōu)化
  • com是什么網(wǎng)站廣告推廣策劃
  • 我的世界做披風(fēng)網(wǎng)站友情鏈接檢測(cè)的特點(diǎn)
  • 松原網(wǎng)站制作如何讓百度收錄自己的網(wǎng)站
  • 婁底網(wǎng)站建設(shè)的話術(shù)北京seo運(yùn)營(yíng)推廣
  • 網(wǎng)站建設(shè)基礎(chǔ)大綱文案軟文推廣有哪些
  • 網(wǎng)站開(kāi)發(fā)用的那些語(yǔ)言怎么在百度發(fā)布自己的文章
  • 花店網(wǎng)站建設(shè)環(huán)境分析百度搜索什么關(guān)鍵詞能搜到網(wǎng)站
  • 午夜做網(wǎng)站營(yíng)銷(xiāo)網(wǎng)站的宣傳、推廣與運(yùn)作
  • 淘寶站內(nèi)推廣方式有哪些班級(jí)優(yōu)化大師使用心得
  • 許昌做網(wǎng)站漢獅網(wǎng)絡(luò)青島seo關(guān)鍵詞優(yōu)化公司
  • 網(wǎng)上建站賺錢(qián)微信公眾號(hào)推廣軟文案例
  • 西安微信公眾號(hào)制作seo優(yōu)化快速排名
  • 網(wǎng)站建設(shè) 中國(guó)聯(lián)盟網(wǎng)百度網(wǎng)頁(yè)版登錄首頁(yè)
  • 怎么把網(wǎng)站提交百度的推廣廣告
  • 模板企業(yè)快速建站關(guān)鍵詞推廣效果分析
  • 青島商業(yè)網(wǎng)站建設(shè)今日油價(jià)92汽油
  • 韓國(guó)網(wǎng)站設(shè)計(jì)風(fēng)格cctv 13新聞?lì)l道
  • 800元做網(wǎng)站哪里做網(wǎng)絡(luò)推廣
  • wordpress所有頁(yè)面溫州網(wǎng)站建設(shè)優(yōu)化
  • 做食品網(wǎng)站需要什么條件品牌廣告策劃方案
  • 訪問(wèn)阿里云主機(jī)網(wǎng)站免費(fèi)的個(gè)人網(wǎng)站怎么做
  • 網(wǎng)站設(shè)計(jì)點(diǎn)評(píng)廣州seo成功案例
  • 西安做網(wǎng)站seo網(wǎng)站seo快速排名優(yōu)化
  • 17做網(wǎng)站廣州沙河地址東莞網(wǎng)站推廣優(yōu)化網(wǎng)站