单词九连猜python编程

编程入门 行业动态 更新时间:2024-10-10 03:27:17

<a href=https://www.elefans.com/category/jswz/34/1769906.html style=单词九连猜python编程"/>

单词九连猜python编程

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实现猜单词小游戏

本文地址: .html

更多推荐

单词九连猜python编程

本文发布于:2024-02-06 06:36:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1747015.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单词   九连猜   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!