您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章主要讲解了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
看完上述内容,是不是对python实现猜单词小游戏的方法有进一步的了解,如果还想学习更多内容,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。