利用python實(shí)現(xiàn)你說我猜游戲的完整實(shí)例
前言
五一參加python技能書的答疑,發(fā)現(xiàn)題目中的邏輯存在問題,對應(yīng)的你說我猜游戲里面的都是恒相等的。就調(diào)試修正了對應(yīng)的代碼,分享給大家,供大家學(xué)習(xí)與娛樂。
一、游戲規(guī)則
請你使用類 KeyValueSet 完成一個(gè)交互式命令行你想我猜游戲。支持:
- 裝載N個(gè)句子對
- 你猜我想 闖關(guān),輸出上半句,等待用戶猜測下半句
- 如果猜中就累加10分,否則扣2分
- 全部結(jié)束輸出用戶本次得分
二、實(shí)現(xiàn)過程
1、基本框架
我們編寫一個(gè)新的class,內(nèi)部通過組合KeyValueSet來支持上述功能,程序框架如下:
# -*- coding: UTF-8 -*-
class GuessSentenceGame:
def __init__(self):
self.kv = KeyValueSet()
self.score = 0
def setup(self, sentences):
# TODO(You): 請?jiān)诖司帉懷b載邏輯
def guess(self, first_word):
# TODO(You): 請?jiān)诖司帉懖聹y結(jié)果,返回 err, value
def run(self):
self.score = 0
for first_word in self.kv.keys():
ret = input("猜一猜下半句是什么? {} -> :".format(first_word))
err, value = self.guess(first_word)
if err==0:
print('你太厲害了,這都能猜得到!+10分!')
self.score += 10
else:
self.score -= 2
print('哈哈,肯定猜不到得啦:{}->{},扣除2分!'.format(first_word, value))
print('游戲結(jié)束,你本次游戲得分:', self.score)
if __name__ == '__main__':
sentences = [
"hello world",
'monkey king',
'tomorrow is another day',
"good bye"
]
game = GuessSentenceGame()
game.setup(sentences)
game.run()一個(gè)示例輸出是:
猜一猜下半句是什么? hello -> :world
你太厲害了,這都能猜得到!+10分!
猜一猜下半句是什么? monkey -> :king
你太厲害了,這都能猜得到!+10分!
猜一猜下半句是什么? tomorrow -> :is another day
你太厲害了,這都能猜得到!+10分!
猜一猜下半句是什么? good -> :a
哈哈,肯定猜不到得啦:good->bye,扣除2分!
游戲結(jié)束,你本次游戲得分: 28
2、完整案例
代碼:
# -*- coding: UTF-8 -*-
class KeyValueSet:
def __init__(self) -> None:
self.dict = {}
def set(self, key, value):
self.dict[key] = value
def get(self, key):
return self.dict.get(key)
def keys(self):
return self.dict.keys()
# -*- coding: UTF-8 -*-
class GuessSentenceGame:
def __init__(self):
self.kv = KeyValueSet()
self.score = 0
#獲取按空格截取的上下句
def setup(self, sentences):
for sentence in sentences:
cut_pos = sentence.find(' ')
first_word, rest = sentence[0:cut_pos], sentence[cut_pos + 1:].strip()
self.kv.set(first_word, rest)
#根據(jù)上句返回對應(yīng)的值,這是原邏輯返回了一個(gè)狀態(tài)碼和下句值,但邏輯存在問題,狀態(tài)碼是恒等于0的因此我們比較輸入值與返回默認(rèn)下句是否一致即可
def guess(self, first_word):
value = self.kv.get(first_word)#
err = 0 if value else 1
#print(err, value)
return err, value
def run(self):
self.score = 0
for first_word in self.kv.keys():
ret = input("猜一猜下半句是什么? {} -> :".format(first_word))
err, value = self.guess(first_word)
if value==ret:#比較輸入值與返回默認(rèn)下句是否一致即可
print('你太厲害了,這都能猜得到!+10分!')
self.score += 10
else:
self.score -= 2
print('哈哈,肯定猜不到得啦:{}->{},扣除2分!'.format(first_word, value))
print('游戲結(jié)束,你本次游戲得分:', self.score)
if __name__ == '__main__':
sentences = [
"他們叫我 技術(shù)總監(jiān)",
'hello world',
'monkey king',
'tomorrow is another day',
'good bye',
"謝謝 大家"
]
game = GuessSentenceGame()
game.setup(sentences)
game.run()效果圖:

三、總結(jié)
到此這篇關(guān)于利用python實(shí)現(xiàn)你說我猜游戲的文章就介紹到這了,更多相關(guān)python你說我猜游戲內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 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í)現(xiàn)猜單詞小游戲
- python簡單猜數(shù)游戲?qū)嵗?/a>
- Python實(shí)現(xiàn)簡單猜數(shù)字游戲
相關(guān)文章
關(guān)于python與opc ua Expert endpoint連接的問題
這篇文章主要介紹了關(guān)于python與opc ua Expert endpoint連接的問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02
Python調(diào)用ChatGPT?API接口的用法詳解
ChatGPT可以實(shí)現(xiàn)chat,生成圖片,識別關(guān)鍵,改錯(cuò)等等功能,本文簡單的給大家介紹一下如何使用python調(diào)用ChatGPT?API接口,感興趣的小伙伴可以參考一下2023-05-05
python__new__內(nèi)置靜態(tài)方法使用解析
這篇文章主要介紹了python__new__內(nèi)置靜態(tài)方法使用解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01
Python enumerate函數(shù)遍歷數(shù)據(jù)對象組合過程解析
這篇文章主要介紹了Python enumerate函數(shù)遍歷數(shù)據(jù)對象組合過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12

