一个很简单的猜单词游戏。

你的机会是有限的,你每猜错一次,你要拯救的小人就出现一部分。

当整个小人出现的时候,游戏结束,你便看到了惨象:一个吊死的屌丝!

可以把单词换成你每天背的单词,便于复习。。。

直接放代码。

#!usr/bin/env python3
#-*- coding:utf-8 -*-import random
try:import cPickle as pickle
except Exception:import pickle# global viriable strokeswith open("strokes.txt",'r') as f:strokes = pickle.loads(f.read().encode('utf-8'))
filename = "2of12inf.txt"def askForInput(word, guessed, pic):"""pic: a string that displays the hangman.word: string, the mystery word.guessed: set, the characters in uper case which user have guessed.this function ask the user guess word until the word is valid."""print(pic)print("Letters already guessed: {}".format(" ".join(list(guessed))))print("The current state of your guess: {}".format(showWord(word, guessed)))characters = frozenset("ABCDEFGHIJKLMNOPQRSTUVWXYZ")while True:ans = input("What is your next letter? (or type your guess) :")ans = ans.strip()ans = ans.upper()if len(ans) > 1:if len(ans) == len(word):if all([ele in characters for ele in ans]):return anselse:print("Invalid entry: enter a word that is a alphabeta string.")else:print("Invalid entry: enter a word that exists {} characters.".format(len(word)))elif len(ans) == 1:if ans in characters:if ans not in guessed:return anselse:print("Invalid entry: enter a single letter you have not already guessed.")else:print("Invalid entry: enter a single letter that is a alphabeta character.")else:print("Invalid entry: enter a single alphabeta letter or guess a  alphabeta word.")def getWordList(minLength, maxLength):"""minLength:int, the min length of the word.maxLength:int, the max length of the word.return: the wordList, a list of strings.this function read the words from text file: 2of12inf.txt."""wordList = []with open(filename, 'r') as f: # iterate the file from top to bottomfor line in f:line = line.strip()line = line.strip("%") # remove pluralsif len(line) >= minLength and len(line) <= maxLength:wordList.append(line)return wordListdef selectWord(minLength, maxLength):"""minLength: int, the min length of the wordmaxLength: int, the max length of the wordreturn: a word in upper case"""wordList = getWordList(minLength, maxLength)word = random.choice(wordList)return word.upper()def showWord(word, guessed):"""word:the upper case of  the mystery word guessed: the set of all guessed charactersreturn : a sstring like "_ _ E E _ E""""word = word.upper()word_hint = ["_" if ele not in guessed else ele for ele in word]return " ".join(word_hint)def printBanner(word):"""word: string, the mystery word.this function print out the number of the target word."""print("YOU WILL GUESS A WORD WITH {} LETTERS!".format(len(word)))def theEnd(word, wrong_times):if wrong_times < len(strokes):print("Congratulations! You win!!")print("The word was {}".format(" ".join(list(word))))else:print("Sorry, you lose!")print("The word was {}".format(" ".join(list(word))))print("Welcome to my blog:")print("---------------------")print("http://blog.csdn.net/myjiayan")print("---------------------")def main():print("WELCOME TO HANGMAN!")print("Welcome to my blog:")print("---------------------")print("http://blog.csdn.net/myjiayan")print("---------------------")minLength = 6maxLength = 8word = selectWord(minLength, maxLength)word = word.upper()printBanner(word)guessed = set()wrong_times = 0# tha main loopwhile wrong_times < len(strokes):# upper case ansans = askForInput(word, guessed, strokes[wrong_times])# ans is an characterif len(ans) == 1:numAns = word.count(ans)print("There is {} {}!".format(numAns, ans))guessed.add(ans)if all([ele in guessed for ele in word]):breakif numAns == 0:wrong_times += 1elif len(ans) == len(word):if ans == word:breakelse:wrong_times += 1print("the word you guessed is wrong!")theEnd(word, wrong_times)if __name__ == "__main__":main()

strokes.txt 和 单词文件 2of12inf.txt 在 http://download.csdn.net/detail/u013805817/9468750 上有下载。

HangMan 猜单词 一个简易的吊死鬼游戏 python版相关推荐

  1. 24点游戏python版

    24点游戏python版 问题描述与规则 24点游戏是经典的纸牌益智游戏.常见游戏规则:从扑克中每次取出4张牌.使用加减乘除,第一个能得出24者为赢.(其中,J代表11,Q代表12,K代表13,A代表 ...

  2. Yii Framework 开发教程(4) Hangman 猜单词游戏实例

    有了前面"Hello,World"的例子和对Yii Framework Web应用基础的介绍,可以开始介绍一个简单而相对而有比较完整的Web应用-Hangman(猜单词游戏),这个 ...

  3. python 2048游戏的设计_Python练习:100行代码实现一个简易的2048游戏

    一.任务目标 1.一个在命令行下的2048游戏 2.主要实现功能:判断游戏是否结束:在随机空白位置生成新的数字:上下左右四个方向上进行平移数字并将能合并的数字进行合并:接收键盘输入:将数字阵列显示到命 ...

  4. JAVA飞机移动斜着走_java复习 做一个简易的飞机游戏

    最近闲来无事复习下java基础,没有特别复杂的功能,一切从简,旨在回顾知识点!!! 游戏效果如下: 我们通过键盘控制飞机前后移动躲避炮弹,如果碰到炮弹则会爆炸,游戏结束并显示游戏坚持的时间 基本的功能 ...

  5. avg在python中的意思_一个简易的AVG游戏模板(python)

    from tkinter import * class Settings: WIDTH = 800 HEIGHT = 600 class Game: def init(self): self.sm = ...

  6. 简易聊天室(Python版)

    目录 简介 运行效果 代码 客户端 服务器 自定义库 通讯协议及相关配置定义库(DY.py) 数据库调用库(SJK.py) 套接字库(TJZ.py) 套接字通讯库(服务器端)(TJZ_FZ.py) 简 ...

  7. 【Python】又一个可视化神器Highcharts,Python版也有哦!

    来源:Python数据之道 作者:Peter 整理:Lemon 本文重点介绍的是可视化库 Highcharts 的相关基础知识,以及如何利用Highcharts来绘制不同场景和需求下的精美柱状图,主要 ...

  8. 又一个可视化神器Highcharts,Python版也有哦!

    来源:Python数据之道 作者:Peter 整理:Lemon 本文重点介绍的是可视化库 Highcharts 的相关基础知识,以及如何利用Highcharts来绘制不同场景和需求下的精美柱状图,主要 ...

  9. 文本清洗?一个工具搞定!Python版 NLP 文本清洗工具

    文本清洗是一个繁琐复杂的工作,不论是对于NLP 的开发者,还是其它领域工作者.这部分工作可以由 JioNLP 工具包一键搞定!!! ⭐ 源码戳这里 => JioNLPhttps://github ...

最新文章

  1. android中XMl文件的读取
  2. 软件开发行业,年轻与大龄程序员的生存现状
  3. ScrollView中嵌套ListView
  4. Spring Cloud Greenwich 正式发布,Hystrix 即将寿终正寝!
  5. [Android]用架构师角度看插件化(1)-Replugin入门剖析
  6. 警惕使用System.Environment.CurrentDirectory遇到的坑
  7. ·XP注册表修改大全
  8. web前端已经不行了?千万不要信
  9. Android 报错:Conversion to Dalvik format failed: ...
  10. 关于Oxite的教训
  11. 把.NET程序部署到没有安装.NET Framwork的机器上
  12. 分享7个超实用的Emmet(zen coding)HTML代码使用技巧
  13. 写给大家看的设计模式 1
  14. Ubuntu18.04 + RTX 2080 Ti 安装NVIDIA显卡驱动的一些坑
  15. 写一程序,用scanf函数输入x,输出y值。
  16. 闪乱神乐台服显示连接不上服务器,忍者大师 闪乱神乐无法连接服务器是什么原因...
  17. sqlmap工具说明
  18. MATLAB如何读取文件中的数据(全网最简便)
  19. “速课小龙”项目冲刺4
  20. 计算机教师面试专业技能部分,教师招聘专业技能测试

热门文章

  1. 计算机网络(三)———数据链路层(w字最强总结)
  2. 此数据库没有有效所有者,因此无法安装数据库关系图支持对象--解决办法
  3. 蓝桥杯-c语言 分解质因数
  4. Android中通过访问本地相册或者相机设置用户头像
  5. CAD制图初学入门教程:一键解决CAD图纸杂乱
  6. 二手手机十分管用的检查方法
  7. php mpdf 设置字体,php 使用mpdf实现指定字段配置字体样式
  8. 【Linux】deb文件如何安装?
  9. HTML5如何将列表向左右移,分类列表左移右移操作
  10. 高校实验室管理的建设思路