HangMan 猜单词 一个简易的吊死鬼游戏 python版
一个很简单的猜单词游戏。
你的机会是有限的,你每猜错一次,你要拯救的小人就出现一部分。
当整个小人出现的时候,游戏结束,你便看到了惨象:一个吊死的屌丝!
可以把单词换成你每天背的单词,便于复习。。。
直接放代码。
#!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版相关推荐
- 24点游戏python版
24点游戏python版 问题描述与规则 24点游戏是经典的纸牌益智游戏.常见游戏规则:从扑克中每次取出4张牌.使用加减乘除,第一个能得出24者为赢.(其中,J代表11,Q代表12,K代表13,A代表 ...
- Yii Framework 开发教程(4) Hangman 猜单词游戏实例
有了前面"Hello,World"的例子和对Yii Framework Web应用基础的介绍,可以开始介绍一个简单而相对而有比较完整的Web应用-Hangman(猜单词游戏),这个 ...
- python 2048游戏的设计_Python练习:100行代码实现一个简易的2048游戏
一.任务目标 1.一个在命令行下的2048游戏 2.主要实现功能:判断游戏是否结束:在随机空白位置生成新的数字:上下左右四个方向上进行平移数字并将能合并的数字进行合并:接收键盘输入:将数字阵列显示到命 ...
- JAVA飞机移动斜着走_java复习 做一个简易的飞机游戏
最近闲来无事复习下java基础,没有特别复杂的功能,一切从简,旨在回顾知识点!!! 游戏效果如下: 我们通过键盘控制飞机前后移动躲避炮弹,如果碰到炮弹则会爆炸,游戏结束并显示游戏坚持的时间 基本的功能 ...
- avg在python中的意思_一个简易的AVG游戏模板(python)
from tkinter import * class Settings: WIDTH = 800 HEIGHT = 600 class Game: def init(self): self.sm = ...
- 简易聊天室(Python版)
目录 简介 运行效果 代码 客户端 服务器 自定义库 通讯协议及相关配置定义库(DY.py) 数据库调用库(SJK.py) 套接字库(TJZ.py) 套接字通讯库(服务器端)(TJZ_FZ.py) 简 ...
- 【Python】又一个可视化神器Highcharts,Python版也有哦!
来源:Python数据之道 作者:Peter 整理:Lemon 本文重点介绍的是可视化库 Highcharts 的相关基础知识,以及如何利用Highcharts来绘制不同场景和需求下的精美柱状图,主要 ...
- 又一个可视化神器Highcharts,Python版也有哦!
来源:Python数据之道 作者:Peter 整理:Lemon 本文重点介绍的是可视化库 Highcharts 的相关基础知识,以及如何利用Highcharts来绘制不同场景和需求下的精美柱状图,主要 ...
- 文本清洗?一个工具搞定!Python版 NLP 文本清洗工具
文本清洗是一个繁琐复杂的工作,不论是对于NLP 的开发者,还是其它领域工作者.这部分工作可以由 JioNLP 工具包一键搞定!!! ⭐ 源码戳这里 => JioNLPhttps://github ...
最新文章
- android中XMl文件的读取
- 软件开发行业,年轻与大龄程序员的生存现状
- ScrollView中嵌套ListView
- Spring Cloud Greenwich 正式发布,Hystrix 即将寿终正寝!
- [Android]用架构师角度看插件化(1)-Replugin入门剖析
- 警惕使用System.Environment.CurrentDirectory遇到的坑
- ·XP注册表修改大全
- web前端已经不行了?千万不要信
- Android 报错:Conversion to Dalvik format failed: ...
- 关于Oxite的教训
- 把.NET程序部署到没有安装.NET Framwork的机器上
- 分享7个超实用的Emmet(zen coding)HTML代码使用技巧
- 写给大家看的设计模式 1
- Ubuntu18.04 + RTX 2080 Ti 安装NVIDIA显卡驱动的一些坑
- 写一程序,用scanf函数输入x,输出y值。
- 闪乱神乐台服显示连接不上服务器,忍者大师 闪乱神乐无法连接服务器是什么原因...
- sqlmap工具说明
- MATLAB如何读取文件中的数据(全网最简便)
- “速课小龙”项目冲刺4
- 计算机教师面试专业技能部分,教师招聘专业技能测试