python實(shí)現(xiàn)簡(jiǎn)單猜單詞游戲
本文實(shí)例為大家分享了python實(shí)現(xiàn)猜單詞游戲的具體代碼,供大家參考,具體內(nèi)容如下
電腦根據(jù)單詞列表隨機(jī)生成一個(gè)單詞,打印出這個(gè)單詞長(zhǎng)度個(gè) ‘ _ ' ,玩家隨機(jī)輸入一個(gè)這個(gè)單詞可能包含的英文字母,如果玩家猜對(duì)了,電腦則會(huì)在正確的空格處填寫(xiě)這個(gè)字母,如果沒(méi)有猜對(duì),游戲次數(shù)就減一。如果玩家在游戲次數(shù)減為零前猜對(duì)這個(gè)單詞的所有字母,則玩家獲勝,否則玩家輸?shù)舯荣悺?/p>
from random import*
words = 'tiger lion wolf elephant zebra ducksheep rabbit mouse'.split()
#得到要猜的神秘單詞
def getWord(wordList):
n = randint(0,len(wordList)-1)
return wordList[n]
#游戲界面
def display(word,wrongLetters,rightLetters,chance):
print('你還有{:n}次機(jī)會(huì)'.format(chance).center(40,'-'))
print('已經(jīng)猜錯(cuò)的字母:'+ wrongLetters)
print()
blanks = '_'*len(word)
for i in range(len(word)):
if word[i] in rightLetters:
blanks = blanks[:i] + word[i] +blanks[i+1:]
for i in blanks:
print(i+' ',end='')
print()
print()
#從玩家的輸入得到一個(gè)猜測(cè)的字母
def getLetter(alreadyGuessed):
while True:
print('請(qǐng)輸入一個(gè)可能的字母:')
guess = input()
guess = guess.lower()
if guess[0] in alreadyGuessed:
print('你已經(jīng)猜過(guò)這個(gè)字母了!')
elif guess[0] not in 'qwertyuiopasdfghjklzxcvbnm':
print('請(qǐng)輸入一個(gè)英文字母!(a-z)')
else:
return guess[0]
#是否再玩一次
def playAgain():
print('是否在玩一次?(y/n)')
s = input()
s = s.lower()
if s[0] == 'y':
return 1
return 0
#游戲初始化
wrongLetters = ''
rightLetters = ''
word = getWord(words)
chance = 6 #初始為6次機(jī)會(huì)
done = False
while True:
display(word,wrongLetters,rightLetters,chance)
guess = getLetter(wrongLetters+rightLetters)
if guess in word:
rightLetters = rightLetters+ guess
foundAll = True
for i in range(len(word)):
if word[i] not in rightLetters:
foundAll = False
break
if foundAll:
print('你真棒,這個(gè)單詞就是'+ word +',你贏了!')
done = True
else:
wrongLetters = wrongLetters + guess
chance = chance - 1
if chance == 0:
display(word,wrongLetters,rightLetters,chance)
print("你已經(jīng)沒(méi)有機(jī)會(huì)了!你一共猜錯(cuò)了"+str(len((wrongLetters))+"次,猜對(duì)了"+str(len(rightLetters))+"次,正確的單詞是:"+ word)
done = True
if done:
if playAgain():
wrongLetters = ''
rightletters = ''
word = getWord(words)
chance = 6 #初始為6次機(jī)會(huì)
done = 0
else:
break
再為大家提供一段代碼:python猜單詞游戲,作為補(bǔ)充,感謝原作者的分享。
import random
WORDS = ("math","english","china","history")
right = 'Y'
print("歡迎參加猜單詞游戲!")
while right=='Y' or right=='y':
word=random.choice(WORDS)
correct=word
newword = ''
while word:
pos=random.randrange(len(word))
newword+=word[pos]
#將word單詞下標(biāo)為pos的字母去掉,取pos前面和后面的字母組成新的word
word = word[:pos]+word[(pos+1):] #保證隨機(jī)字母出現(xiàn)不會(huì)重復(fù)
print("你要猜測(cè)的單詞為:",newword)
guess = input("請(qǐng)輸入你的答案:")
count=1
while count<5:
if guess!=correct:
guess = input("輸入的單詞錯(cuò)誤,請(qǐng)重新輸入:")
count+=1
else :
print("輸入的單詞正確,正確單詞為:",correct)
break
if count == 5:
print("您已猜錯(cuò)5次,正確的單詞為:",correct)
right = input("是否繼續(xù),Y/N:")
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python簡(jiǎn)單實(shí)現(xiàn)9宮格圖片實(shí)例
在本篇內(nèi)容里小編給各位分享的是一篇關(guān)于python實(shí)現(xiàn)朋友圈中的九宮格圖片的實(shí)例講解,有需要的朋友們可以參考下。2020-09-09
python更換國(guó)內(nèi)鏡像源三種實(shí)用方法
這篇文章主要給大家介紹了關(guān)于python更換國(guó)內(nèi)鏡像源三種實(shí)用方法的相關(guān)資料,更換Python鏡像源可以幫助解決使用pip安裝包時(shí)速度過(guò)慢或無(wú)法連接的問(wèn)題,需要的朋友可以參考下2023-09-09
python中urllib.unquote亂碼的原因與解決方法
這篇文章主要給大家介紹了python中urllib.unquote亂碼的原因與解決方法,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考價(jià)值,需要的朋友可以參考學(xué)習(xí),下面跟著小編一起來(lái)學(xué)習(xí)學(xué)習(xí)吧。2017-04-04
在GitHub Pages上使用Pelican搭建博客的教程
這篇文章主要介紹了在GitHub Pages上使用Pelican搭建博客的教程,Pelican是一個(gè)使用Python實(shí)現(xiàn)的開(kāi)源博客系統(tǒng),需要的朋友可以參考下2015-04-04
關(guān)于Python錯(cuò)誤重試方法總結(jié)
在本篇文章里小編給網(wǎng)友們分享一篇關(guān)于關(guān)于Python錯(cuò)誤重試方法總結(jié)內(nèi)容,有需要的朋友們跟著學(xué)習(xí)參考下。2021-01-01
詳解解Django 多對(duì)多表關(guān)系的三種創(chuàng)建方式
本文主要介紹了詳解解Django 多對(duì)多表關(guān)系的三種創(chuàng)建方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-08-08
Python3.6簡(jiǎn)單操作Mysql數(shù)據(jù)庫(kù)
這篇文章主要為大家詳細(xì)介紹了Python3.6簡(jiǎn)單操作Mysql數(shù)據(jù)庫(kù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09
python讀取excel數(shù)據(jù)并且畫(huà)圖的實(shí)現(xiàn)示例
這篇文章主要介紹了python讀取excel數(shù)據(jù)并且畫(huà)圖的實(shí)現(xiàn)示例,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2021-02-02
python3.7 openpyxl 在excel單元格中寫(xiě)入數(shù)據(jù)實(shí)例
這篇文章主要介紹了python3.7 openpyxl 在excel單元格中寫(xiě)入數(shù)據(jù)實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09

