python realizes simple word guessing game
- 2021-08-31 08:46:12
- OfStack
This article example for everyone to share python guessing word game specific code, for your reference, the specific content is as follows
The computer randomly generates a word according to the word list, prints out the length of the word, and the player randomly inputs an English letter that may be contained in the word. If the player guesses correctly, the computer will fill in the letter in the correct space. If not, the number of games will be reduced by 1. If the player guesses all the letters of the word correctly before the number of games is reduced to zero, the player wins, otherwise the player loses the game.
from random import*
words = 'tiger lion wolf elephant zebra ducksheep rabbit mouse'.split()
# Get the mysterious words to guess
def getWord(wordList):
n = randint(0,len(wordList)-1)
return wordList[n]
# Game interface
def display(word,wrongLetters,rightLetters,chance):
print(' You still have {:n} A second chance '.format(chance).center(40,'-'))
print(' Letters that have been guessed wrong :'+ 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()
# Get from the player's input 1 A guessed letter
def getLetter(alreadyGuessed):
while True:
print(' Please enter 1 Possible letters :')
guess = input()
guess = guess.lower()
if guess[0] in alreadyGuessed:
print(' You have guessed this letter! ')
elif guess[0] not in 'qwertyuiopasdfghjklzxcvbnm':
print(' Please enter 1 English letters! (a-z)')
else:
return guess[0]
# Do you want to play again 1 Times
def playAgain():
print(' Are you playing 1 Times? (y/n)')
s = input()
s = s.lower()
if s[0] == 'y':
return 1
return 0
# Game initialization
wrongLetters = ''
rightLetters = ''
word = getWord(words)
chance = 6 # Initially 6 A second chance
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(' You are great. This word is '+ word +' You win! ')
done = True
else:
wrongLetters = wrongLetters + guess
chance = chance - 1
if chance == 0:
display(word,wrongLetters,rightLetters,chance)
print(" You have no chance! You 1 A total of guesses are wrong "+str(len((wrongLetters))+" Twice, you guessed it right "+str(len(rightLetters))+" The correct word is: "+ word)
done = True
if done:
if playAgain():
wrongLetters = ''
rightletters = ''
word = getWord(words)
chance = 6 # Initially 6 A second chance
done = 0
else:
break
To provide you with a code: python guessing word game, as a supplement, thank the original author for sharing.
import random
WORDS = ("math","english","china","history")
right = 'Y'
print(" Welcome to the word guessing game! ")
while right=='Y' or right=='y':
word=random.choice(WORDS)
correct=word
newword = ''
while word:
pos=random.randrange(len(word))
newword+=word[pos]
# Will word Words are subscripted to pos Remove the letters of, take pos The front and back letters make up the new word
word = word[:pos]+word[(pos+1):] # Ensure that random letters will not be repeated
print(" The words you want to guess are: ",newword)
guess = input(" Please enter your answer: ")
count=1
while count<5:
if guess!=correct:
guess = input(" Wrong words entered, please re-enter: ")
count+=1
else :
print(" The words entered are correct. The correct words are: ",correct)
break
if count == 5:
print(" You have guessed wrong 5 The correct words are: ",correct)
right = input(" Whether to continue, Y/N : ")