python實(shí)現(xiàn)猜單詞小游戲
Python初學(xué)者小游戲:猜單詞,供大家參考,具體內(nèi)容如下
游戲邏輯:就像我們?cè)?jīng)英語學(xué)習(xí)機(jī)上的小游戲一樣,電腦會(huì)從事先預(yù)置的詞庫中抽取單詞,然后給出單詞的字母數(shù)量,給定猜解次數(shù),然后讓玩家進(jìn)行猜測(cè),并給出每次猜測(cè)的正確字母與錯(cuò)誤字母。
涉及知識(shí)點(diǎn):random.randint(),print(),input()(raw_input())
參考實(shí)現(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']
#單詞隨機(jī)選擇函數(shù)
def getRandomWord():
global Words
return Words[random.randint(0,len(Words)-1)]
#猜測(cè)流程
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
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- python實(shí)現(xiàn)的簡單猜數(shù)字游戲
- python實(shí)現(xiàn)猜數(shù)字游戲
- python實(shí)現(xiàn)猜拳小游戲
- python3.3使用tkinter開發(fā)猜數(shù)字游戲示例
- python實(shí)現(xiàn)猜數(shù)字小游戲
- Python實(shí)現(xiàn)的搖骰子猜大小功能小游戲示例
- python實(shí)現(xiàn)猜數(shù)字游戲(無重復(fù)數(shù)字)示例分享
- python簡單猜數(shù)游戲?qū)嵗?/a>
- Python實(shí)現(xiàn)簡單猜數(shù)字游戲
- 利用python實(shí)現(xiàn)你說我猜游戲的完整實(shí)例
相關(guān)文章
Python字符串和正則表達(dá)式中的反斜杠(''\'')問題詳解
在本篇文章里小編給大家整理的是關(guān)于Python字符串和正則表達(dá)式中的反斜杠('\')問題以及相關(guān)知識(shí)點(diǎn),有需要的朋友們可以學(xué)習(xí)下。2019-09-09
Python實(shí)現(xiàn)實(shí)時(shí)增量數(shù)據(jù)加載工具的解決方案
這篇文章主要分享結(jié)合單例模式實(shí)際應(yīng)用案例:實(shí)現(xiàn)實(shí)時(shí)增量數(shù)據(jù)加載工具的解決方案。最關(guān)鍵的是實(shí)現(xiàn)一個(gè)可進(jìn)行添加、修改、刪除等操作的增量ID記錄表。需要的可以參考一下2022-02-02
Python 實(shí)現(xiàn)一個(gè)簡單的web服務(wù)器
這篇文章主要介紹了Python 實(shí)現(xiàn)一個(gè)簡單的web服務(wù)器的方法,幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下2021-01-01
pycharm中import呈現(xiàn)灰色原因的解決方法
這篇文章主要介紹了pycharm中import呈現(xiàn)灰色原因的解決方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
通過Python實(shí)現(xiàn)一個(gè)A/B測(cè)試詳解
A/B測(cè)試,通過分析兩種不同的營銷策略,以此來選擇最佳的營銷策略,可以高效地將流量轉(zhuǎn)化為銷售額。本文主要介紹了如何通過Python實(shí)現(xiàn)一個(gè)A/B測(cè)試,感興趣的可以了解一下2023-01-01

