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实现的飞速中文网小说下载脚本
Apr 23 Python
Python中type的构造函数参数含义说明
Jun 21 Python
Python日期时间Time模块实例详解
Apr 15 Python
Django框架文件上传与自定义图片上传路径、上传文件名操作分析
May 10 Python
自定义django admin model表单提交的例子
Aug 23 Python
python sorted方法和列表使用解析
Nov 18 Python
python字符串,元组,列表,字典互转代码实例详解
Feb 14 Python
Pytorch高阶OP操作where,gather原理
Apr 30 Python
如何基于pandas读取csv后合并两个股票
Sep 25 Python
Django xadmin安装及使用详解
Oct 26 Python
彻底解决Python包下载慢问题
Nov 15 Python
Python学习开发之图形用户界面详解
Aug 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
VB中的RasEnumConnections函数返回632错误解决方法
2014/07/29 PHP
php 魔术方法详解
2014/11/11 PHP
php中file_exists函数使用详解
2015/05/08 PHP
PHP实现的简单分页类及用法示例
2016/05/06 PHP
PHPMailer ThinkPHP实现自动发送邮件功能
2018/06/10 PHP
关于UTF-8的客户端用AJAX方式获取GB2312的服务器端乱码问题的解决办法
2010/11/30 Javascript
百度地图api如何使用
2015/08/03 Javascript
jQuery实现默认是闭合的FAQ展开效果菜单
2015/09/14 Javascript
JavaScript正则表达式的分组匹配详解
2016/02/13 Javascript
老生常谈JavaScript 函数表达式
2016/09/01 Javascript
完美解决IE9浏览器出现的对象未定义问题
2016/09/29 Javascript
react.js 翻页插件实例代码
2017/01/19 Javascript
vue-cli构建项目下使用微信分享功能
2018/05/28 Javascript
Vue项目部署在Spring Boot出现页面空白问题的解决方案
2018/11/26 Javascript
JavaScript中的&quot;=、==、===&quot;区别讲解
2019/01/22 Javascript
JS addEventListener()和attachEvent()方法实现注册事件
2021/01/11 Javascript
Python绑定方法与非绑定方法详解
2017/08/18 Python
PyQt5每天必学之事件与信号
2018/04/20 Python
Python 识别12306图片验证码物品的实现示例
2020/01/20 Python
python实现在内存中读写str和二进制数据代码
2020/04/24 Python
美国网上眼镜商城:Zenni Optical
2016/11/20 全球购物
匈牙利墨盒和碳粉购买网站:CDRmarket
2018/04/14 全球购物
实习护理工作自我评价
2013/09/25 职场文书
代理商会议邀请函
2014/01/27 职场文书
犯错检讨书
2014/02/21 职场文书
协议书范本
2014/04/23 职场文书
学生操行评语大全
2014/04/24 职场文书
征兵宣传标语
2014/06/20 职场文书
会计岗位说明书
2014/07/29 职场文书
国家助学金感谢信
2015/01/21 职场文书
2015年全国爱耳日活动总结
2015/02/27 职场文书
圣诞晚会主持词开场白
2015/05/28 职场文书
大学入学感言
2015/08/01 职场文书
公开致歉信
2019/06/24 职场文书
广告策划的实习心得体会总结!
2019/07/22 职场文书
用CSS3画一个爱心
2021/04/27 HTML / CSS