国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python實現(xiàn)詩歌游戲(類繼承)

2020-02-16 01:22:56
字體:
來源:轉載
供稿:網(wǎng)友

本文實例為大家分享了python實現(xiàn)詩歌游戲的具體代碼,供大家參考,具體內容如下

具體游戲有:根據(jù)上句猜下句、猜作者、猜朝代、猜詩名等

如果有更好玩兒的游戲,不妨自己寫一下

1.首先,先把搜集到的詩歌全部放到一個txt文件下,命名為poems.txt

2.其次,再定義一個poem類,執(zhí)行的時候輸出詩歌的名字,作者,朝代等,代碼如下:

class Poem:   def __init__(self):    self.title = ''    self.dynasty = ''    self.author = ''    self.sentences = []   def __str__(self):    return '{}/n{}/n{}/n{}'.format(      self.title, self.dynasty, self.author, '/n'.join(self.sentences))

3.加載出來詩歌的所有部分,代碼如下:

from enum import Enumfrom poem import Poem  Poems = []  def load():  class ReadState(Enum):    Nothing = 0    Title = 1    DynastyAndAuthor = 2    Sentences = 3   print('開始加載詩歌')   f = open('poems.txt', encoding='utf-8')  lines = f.readlines()   state = ReadState.Title  poem = None   for ln in lines:    line = ln.strip()    if len(line) > 0:      try:        if line.startswith('-'):          if poem is not None:            Poems.append(poem)            print('.', end='')          poem = Poem()          poem.title = line.lstrip('-')          state = ReadState.DynastyAndAuthor          continue        if state == ReadState.DynastyAndAuthor:          dynasty_author = line.split(' ')          poem.dynasty = dynasty_author[0]          poem.author = dynasty_author[-1]          state = ReadState.Sentences          continue        if state == ReadState.Sentences:          poem.sentences.append(line)      except IndexError as e:        print(line)  print('/n共加載{}首詩歌'.format(len(Poems)))  print()  load()

4.下面開始寫具體的功能代碼,以猜朝代和猜下句為例。

(1)猜朝代代碼如下

# -*- coding: utf-8 -*-__author__ = 'wj'__date__ = '2018/7/20 9:54' from game import Game  class DynastyGame(Game):   def __init__(self, poems):    super(DynastyGame, self).__init__(poems)    self.name = '猜朝代'    self.rules = '根據(jù)詩歌猜作者所處的朝代,猜對加10分,猜錯不扣分。'   def design_question(self):    """設計問題及答案"""    self.answer = self.poem.dynasty     # 打印題目    print(self.poem.title)    print(self.poem.author)    print()    # enumerate() 會將列表中的索引和數(shù)據(jù)一一對應取出    # i 數(shù)據(jù)的索引  s數(shù)據(jù)    for i, s in enumerate(self.poem.sentences):      print(s)      if i > 5:        print('...')        break    print()   def get_answer(self):    """獲取答案"""    # 1.輸出問題    print('這首詩的作者是:{}'.format(self.poem.author))     while True:      self.user_answer = input('請輸入他/她所在的朝代:')      # 2.判斷是否輸入了內容      if self.user_answer:        break   def judge(self):    """判斷答案"""    is_ok = super(DynastyGame, self).judge()     if is_ok:      self.score += 10       print('回答正確!')    else:      print('回答錯誤!')      print('{}所在的朝代是:{}'.format(self.poem.author, self.poem.dynasty))     print('您目前的得分為:{}'.format(self.score))  if __name__ == '__main__':  from load_poems import Poems  dynasty = DynastyGame(Poems)  dynasty.run()             
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 北宁市| 陵川县| 土默特左旗| 灵石县| 稷山县| 棋牌| 岳西县| 金沙县| 内乡县| 新龙县| 元江| 兰考县| 南江县| 定陶县| 郸城县| 宁都县| 伊通| 资讯 | 得荣县| 岳阳市| 福州市| 和田市| 华蓥市| 恩平市| 定结县| 横峰县| 察哈| 双辽市| 瑞金市| 锦州市| 诸城市| 吴川市| 齐河县| 神农架林区| 海淀区| 东兴市| 图片| 建宁县| 浠水县| 宁陕县| 建水县|