python实现猜单词小游戏
Python初学者小游戏:猜单词,供大家参考,具体内容如下
游戏逻辑:就像我们曾经英语学习机上的小游戏一样,电脑会从事先预置的词库中抽取单词,然后给出单词的字母数量,给定猜解次数,然后让玩家进行猜测,并给出每次猜测的正确字母与错误字母。
涉及知识点:random.randint(),print(),input()(raw_input())
参考实现代码:
#!/usr/bin/python
#-*-coding:utf-8-*-
from__future__importprint_function
importos
importsys
importrandom
importtime
#单词库
Words=['apple','pear','banana']
#单词随机选择函数
defgetRandomWord():
globalWords
returnWords[random.randint(0,len(Words)-1)]
#猜测流程
defgetGuess():
whileTrue:
guess=raw_input("GuesstheWord:")
forletteringuess:
ifletterinwrongLetters:
print("Thechar:"+letter+"youhavealreadyguessed")
continue
break
returnguess
#判别显示流程
defdisplayGame(secretLetters,wrongLetters,secretWord):
globalguess
globalcount
print("Info:")
forletteringuess:
ifletterinsecretWord:
secretLetters+=letter
else:
wrongLetters+=letter
print("SecretLetters:",end='')
forletterinsecretLetters:
print(letter,end='')
print()
print("WrongLetters:",end='')
forletterinwrongLetters:
print(letter,end='')
print()
print("Count:"+str(count))
blanks='_'*len(secretWord)
foriinrange(len(guess)):
ifi>=len(secretWord):
break
ifsecretWord[i]==guess[i]:
blanks=blanks[:i]+secretWord[i]+blanks[i+1:]
print("Word:",end='')
foriinblanks:
print(i,end="")
print()
print()
#主流程
secretLetters=''
wrongLetters=''
secretWord=''
guess=""
count=6
os.system('cls')
secretWord=getRandomWord()
whileTrue:
displayGame(secretLetters,wrongLetters,secretWord)
guess=getGuess()
ifguess==secretWord:
print("Youwin!")
break
else:
ifcount<=0:
print("Youlose!")
break
else:
count-=1
continue
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下