python实现简单猜单词游戏


Posted in Python onDecember 24, 2020

本文实例为大家分享了python实现猜单词游戏的具体代码,供大家参考,具体内容如下

电脑根据单词列表随机生成一个单词,打印出这个单词长度个 ‘ _ ' ,玩家随机输入一个这个单词可能包含的英文字母,如果玩家猜对了,电脑则会在正确的空格处填写这个字母,如果没有猜对,游戏次数就减一。如果玩家在游戏次数减为零前猜对这个单词的所有字母,则玩家获胜,否则玩家输掉比赛。

from random import*
words = 'tiger lion wolf elephant zebra ducksheep rabbit mouse'.split()
 
#得到要猜的神秘单词
def getWord(wordList):
 n = randint(0,len(wordList)-1)
 return wordList[n]
 
#游戏界面
def display(word,wrongLetters,rightLetters,chance):
 print('你还有{:n}次机会'.format(chance).center(40,'-'))
 print('已经猜错的字母:'+ wrongLetters)
 print()
 blanks = '_'*len(word)
 for i in range(len(word)):
  if word[i] in rightLetters:
   blanks = blanks[:i] + word[i] +blanks[i+1:]
 for i in blanks:
  print(i+' ',end='')
 print()
 print()
 
#从玩家的输入得到一个猜测的字母
def getLetter(alreadyGuessed):
 while True:
  print('请输入一个可能的字母:')
  guess = input()
  guess = guess.lower()
  if guess[0] in alreadyGuessed:
   print('你已经猜过这个字母了!')
  elif guess[0] not in 'qwertyuiopasdfghjklzxcvbnm':
   print('请输入一个英文字母!(a-z)')
  else:
   return guess[0]
  
#是否再玩一次
def playAgain():
 print('是否在玩一次?(y/n)')
 s = input()
 s = s.lower()
 if s[0] == 'y':
  return 1
 return 0
 
#游戏初始化
wrongLetters = ''
rightLetters = ''
word = getWord(words)
chance = 6 #初始为6次机会
done = False
 
while True:
 display(word,wrongLetters,rightLetters,chance)
 
 guess = getLetter(wrongLetters+rightLetters)
 
 if guess in word:
  rightLetters = rightLetters+ guess
  foundAll = True
  for i in range(len(word)):
   if word[i] not in rightLetters:
    foundAll = False
    break
  if foundAll:
   print('你真棒,这个单词就是'+ word +',你赢了!')
   done = True
 else:
   wrongLetters = wrongLetters + guess
   chance = chance - 1
   if chance == 0:
    display(word,wrongLetters,rightLetters,chance)
    print("你已经没有机会了!你一共猜错了"+str(len((wrongLetters))+"次,猜对了"+str(len(rightLetters))+"次,正确的单词是:"+ word)
    done = True
 if done:
  if playAgain():
   wrongLetters = ''
   rightletters = ''
   word = getWord(words)
   chance = 6 #初始为6次机会
   done = 0
  else:
   break

再为大家提供一段代码:python猜单词游戏,作为补充,感谢原作者的分享。

import random
WORDS = ("math","english","china","history")
right = 'Y'
print("欢迎参加猜单词游戏!")
 
while right=='Y' or right=='y':
  word=random.choice(WORDS)
  correct=word
  newword = ''
  while word:
    pos=random.randrange(len(word))
    newword+=word[pos]
    #将word单词下标为pos的字母去掉,取pos前面和后面的字母组成新的word
    word = word[:pos]+word[(pos+1):] #保证随机字母出现不会重复
  print("你要猜测的单词为:",newword)
  guess = input("请输入你的答案:")
  count=1
  while count<5:
    if guess!=correct:
      guess = input("输入的单词错误,请重新输入:")
      count+=1
    else :
      print("输入的单词正确,正确单词为:",correct)
      break
  if count == 5:
    print("您已猜错5次,正确的单词为:",correct)
 
  right = input("是否继续,Y/N:")

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python操作gmail实例
Jan 14 Python
Python算法应用实战之栈详解
Feb 04 Python
python实现K最近邻算法
Jan 29 Python
PyTorch学习笔记之回归实战
May 28 Python
django基于存储在前端的token用户认证解析
Aug 06 Python
Numpy对数组的操作:创建、变形(升降维等)、计算、取值、复制、分割、合并
Aug 28 Python
python代码实现TSNE降维数据可视化教程
Feb 28 Python
python 使用事件对象asyncio.Event来同步协程的操作
May 04 Python
python实现磁盘日志清理的示例
Nov 05 Python
使用Python制作一盏 3D 花灯喜迎元宵佳节
Feb 26 Python
python实现过滤敏感词
May 08 Python
Python何绘制带有背景色块的折线图
Apr 23 Python
Python 虚拟环境工作原理解析
Dec 24 #Python
python基于openpyxl生成excel文件
Dec 23 #Python
Python+unittest+requests+excel实现接口自动化测试框架
Dec 23 #Python
用python计算文件的MD5值
Dec 23 #Python
python中lower函数实现方法及用法讲解
Dec 23 #Python
Python类型转换的魔术方法详解
Dec 23 #Python
python3 googletrans超时报错问题及翻译工具优化方案 附源码
Dec 23 #Python
You might like
yii框架builder、update、delete使用方法
2014/04/30 PHP
typecho插件编写教程(四):插件挂载
2015/05/28 PHP
php分页原理 分页代码 分页类制作教程
2016/09/23 PHP
PHP中OpenSSL加密问题整理
2017/12/14 PHP
laravel在中间件内生成参数并且传递到控制器中的2种姿势
2019/10/15 PHP
jquery mobile动态添加元素之后不能正确渲染解决方法说明
2014/03/05 Javascript
js动态改变select选择变更option的index值示例
2014/07/10 Javascript
angularjs实现多张图片上传并预览功能
2017/02/24 Javascript
基于vue中keep-alive缓存问题的解决方法
2018/09/21 Javascript
vuex 实现getter值赋值给vue组件里的data示例
2019/11/05 Javascript
vue 子组件watch监听不到prop的解决
2020/08/09 Javascript
js实现头像上传并且可预览提交
2020/12/25 Javascript
[58:12]Ti4第二日主赛事败者组 LGD vs iG 3
2014/07/21 DOTA
使用python实现baidu hi自动登录的代码
2013/02/10 Python
python使用PyV8执行javascript代码示例分享
2013/12/04 Python
分析python动态规划的递归、非递归实现
2018/03/04 Python
python爬虫之自动登录与验证码识别
2020/06/15 Python
详解用python计算阶乘的几种方法
2019/08/14 Python
Python如何解除一个装饰器
2020/08/07 Python
Python基于staticmethod装饰器标示静态方法
2020/10/17 Python
python IP地址转整数
2020/11/20 Python
日本卡普空电视游戏软件公司官方购物网站:e-CAPCOM
2018/07/17 全球购物
英国领先的互联网葡萄酒礼品商:Vintage Wine & Port
2019/05/24 全球购物
网吧收银员岗位职责
2013/12/14 职场文书
建筑个人求职信范文
2014/01/25 职场文书
个人自我剖析材料
2014/02/07 职场文书
金融管理毕业生求职信
2014/03/03 职场文书
还款承诺书范文
2014/05/20 职场文书
运动会稿件100字
2014/09/24 职场文书
2014年高中班主任工作总结
2014/11/08 职场文书
教师个人师德总结
2015/02/06 职场文书
个人自我鉴定怎么写?
2019/07/01 职场文书
会议承办单位欢迎词
2019/07/09 职场文书
python面向对象版学生信息管理系统
2021/06/24 Python
一文彻底理解js原生语法prototype,__proto__和constructor
2021/10/24 Javascript
MySQL 原理优化之Group By的优化技巧
2022/08/14 MySQL