網(wǎng)站首頁 編程語言 正文
前言
五一參加python技能書的答疑,發(fā)現(xiàn)題目中的邏輯存在問題,對應(yīng)的你說我猜游戲里面的都是恒相等的。就調(diào)試修正了對應(yīng)的代碼,分享給大家,供大家學(xué)習(xí)與娛樂。
一、游戲規(guī)則
請你使用類 KeyValueSet 完成一個交互式命令行你想我猜游戲。支持:
- 裝載N個句子對
- 你猜我想?闖關(guān),輸出上半句,等待用戶猜測下半句
- 如果猜中就累加10分,否則扣2分
- 全部結(jié)束輸出用戶本次得分
二、實現(xiàn)過程
1、基本框架
我們編寫一個新的class,內(nèi)部通過組合KeyValueSet來支持上述功能,程序框架如下:
# -*- coding: UTF-8 -*-
class GuessSentenceGame:
def __init__(self):
self.kv = KeyValueSet()
self.score = 0
def setup(self, sentences):
# TODO(You): 請在此編寫裝載邏輯
def guess(self, first_word):
# TODO(You): 請在此編寫猜測結(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()
一個示例輸出是:
猜一猜下半句是什么? 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)的值,這是原邏輯返回了一個狀態(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é)
原文鏈接:https://blog.csdn.net/qq_29061315/article/details/124577564
相關(guān)推薦
- 2023-01-01 C++日期和時間編程小結(jié)_C 語言
- 2022-10-13 Python教程之類型轉(zhuǎn)換詳解_python
- 2022-06-12 超詳細(xì)分析C語言動態(tài)內(nèi)存管理問題_C 語言
- 2022-09-16 Firewalld防火墻安全防護(hù)_網(wǎng)絡(luò)安全
- 2023-11-11 【LaTex】如何輸入英文字母的花體字?花體字最全總結(jié)
- 2023-02-04 Rust?語言的全鏈路追蹤庫?tracing使用方法_Rust語言
- 2022-09-25 注解@Autowired如何自動裝配
- 2024-03-21 微信支付的簽名算法
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支