這篇文章主要講解了python實現(xiàn)猜單詞小游戲的方法,內(nèi)容清晰明了,對此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會有幫助。
建網(wǎng)站原本是網(wǎng)站策劃師、網(wǎng)絡(luò)程序員、網(wǎng)頁設(shè)計師等,應(yīng)用各種網(wǎng)絡(luò)程序開發(fā)技術(shù)和網(wǎng)頁設(shè)計技術(shù)配合操作的協(xié)同工作。創(chuàng)新互聯(lián)建站專業(yè)提供成都網(wǎng)站制作、網(wǎng)站設(shè)計、外貿(mào)網(wǎng)站建設(shè),網(wǎng)頁設(shè)計,網(wǎng)站制作(企業(yè)站、成都響應(yīng)式網(wǎng)站建設(shè)公司、電商門戶網(wǎng)站)等服務(wù),從網(wǎng)站深度策劃、搜索引擎友好度優(yōu)化到用戶體驗的提升,我們力求做到極致!游戲邏輯:就像我們曾經(jīng)英語學(xué)習(xí)機上的小游戲一樣,電腦會從事先預(yù)置的詞庫中抽取單詞,然后給出單詞的字母數(shù)量,給定猜解次數(shù),然后讓玩家進行猜測,并給出每次猜測的正確字母與錯誤字母。
涉及知識點:random.randint(),print(),input()(raw_input())
參考實現(xiàn)代碼:
#!/usr/bin/python # -*- coding: utf-8 -*- from __future__ import print_function import os import sys import random import time #單詞庫 Words = ['apple','pear','banana'] #單詞隨機選擇函數(shù) def getRandomWord(): global Words return Words[random.randint(0,len(Words)-1)] #猜測流程 def getGuess(): while True: guess = raw_input("Guess the Word: ") for letter in guess: if letter in wrongLetters: print("The char: " + letter + " you have already guessed") continue break return guess #判別顯示流程 def displayGame(secretLetters,wrongLetters,secretWord): global guess global count print("Info: ") for letter in guess: if letter in secretWord: secretLetters += letter else: wrongLetters += letter print("SecretLetters: ",end = '') for letter in secretLetters: print(letter,end = ' ') print() print("WrongLetters: ",end = '') for letter in wrongLetters: print(letter,end = ' ') print() print("Count: "+str(count)) blanks = '_'*len(secretWord) for i in range(len(guess)): if i >=len(secretWord): break if secretWord[i]==guess[i]: blanks = blanks[:i] + secretWord[i] + blanks[i+1:] print("Word: ",end = '') for i in blanks: print(i,end=" ") print() print() #主流程 secretLetters = '' wrongLetters = '' secretWord = '' guess = "" count = 6 os.system('cls') secretWord = getRandomWord() while True: displayGame(secretLetters,wrongLetters,secretWord) guess = getGuess() if guess == secretWord: print ("You win !") break else: if count <= 0: print("You lose !") break else: count -= 1 continue
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。