Python初学者小游戏:猜单词


游戏逻辑就像我们曾经英语学习机上的小游戏一样,电脑会从事先预置的词库中抽取单词,然后给出单词的字母数量,给定猜解次数,然后让玩家进行猜测,并给出每次猜测的正确字母与错误字母。


涉及知识点random.randint(),print(),input()(raw_input())

参考实现代码:


#!/usr/bin/python
# -*- coding: utf-8 -*- from __future__ import print_function
import os
import sys
import random
import time #单词库
Words = ['apple','pear','banana'] #单词随机选择函数
def getRandomWord():
global Words
return Words[random.randint(0,len(Words)-1)] #猜测流程
def getGuess():
while True:
guess = raw_input("Guess the Word: ")
for letter in guess:
if letter in wrongLetters:
print("The char: " + letter + " you have already guessed")
continue break
return guess #判别显示流程
def displayGame(secretLetters,wrongLetters,secretWord):
global guess
global count
print("Info: ")
for letter in guess:
if letter in secretWord:
secretLetters += letter
else:
wrongLetters += letter print("SecretLetters: ",end = '')
for letter in secretLetters:
print(letter,end = ' ')
print() print("WrongLetters: ",end = '')
for letter in wrongLetters:
print(letter,end = ' ')
print()
print("Count: "+str(count))
blanks = '_'*len(secretWord)
for i in range(len(guess)):
if i >=len(secretWord):
break
if secretWord[i]==guess[i]:
blanks = blanks[:i] + secretWord[i] + blanks[i+1:]
print("Word: ",end = '')
for i in blanks:
print(i,end=" ")
print()
print() #主流程 secretLetters = ''
wrongLetters = ''
secretWord = ''
guess = ""
count = 6 os.system('cls')
secretWord = getRandomWord()
while True:
displayGame(secretLetters,wrongLetters,secretWord)
guess = getGuess()
if guess == secretWord:
print ("You win !")
break
else:
if count <= 0:
print("You lose !")
break
else:
count -= 1
continue

最新文章

  1. 用netbeans和xdebug调试php的配置
  2. 指针与数组的区别 —— 《C语言深度剖析》读书心得
  3. 悲惨记忆。。QImage之 pixel() &amp;&amp; setPixel()参数不要给反了。。。
  4. LEETCODE —— Populating Next Right Pointers in Each Node
  5. HTML5+CSS3+jquery实现简单的音乐播放器
  6. eclipse的使用一
  7. POJ 2104&amp;HDU 2665 Kth number(主席树入门+离散化)
  8. linux configure
  9. 最大子序列和(O(n))
  10. 【Java集合框架】规则集--Set
  11. HDU 5675 ztr loves math (数学推导)
  12. POJ 2185 Milking Grid(KMP)
  13. 问题-Delphi记忆工程打开的单元(XE2设置项)
  14. DevExpress之lookupedit
  15. gir配置
  16. HDOJ 1043 Eight(A* 搜索)
  17. iOS开发 日常错误积累
  18. 《java入门第一季》之根据小案例体会泛型
  19. Java基础之理解Annotation
  20. Eclipse中执行Maven命令时控制台输出乱码

热门文章

  1. 1.5 PHP基础+1.5.1 访问数据库
  2. Android - Handler原理
  3. POJ_2387 Til the Cows Come Hom 【最短路】
  4. 攻防世界 reverse seven
  5. IT培训有哪些坑(三)?
  6. 鸿蒙运行报错:Failure[INSTALL_PARSE_FAILED_USESDK_ERROR] Error while Deploying HAP
  7. 截取pod ip地址最后一列
  8. 三、python学习-常用模块
  9. vscode(Visual Studio Code)中安装Debugger for Chrome插件调试页面
  10. springboot项目整合mybatis