Hangman是一款文字游戏,其中计算机将从字典中随机选择一个单词,玩家必须在给定的转数中正确猜测它。要猜的词用一排星代表。如果猜到的字母是单词,脚本将自动放置到正确的位置。
这是代码words.txt中使用的文本文件,其中包含50,000个英文单词。
需要的模块::
import random
# Python program to implement Hangman game
# Importing random module
import random
# Function to randomly select
# a word from dictionary
def get_word():
# Path to the text file
with open('/Users/Admin/Desktop/words.txt', 'r') as f:
# Reads each word after splitting
words1 = f.read().splitlines()
# Returns any random word
return random.choice(words1)
myword = get_word()
# Function prints row of
# stars in place of words
for i in myword:
print("*", end = " ")
# Calculating length of word
l = len(myword)
print("\nWord has %d letters" %l)
# Check if entered letter is correct
def check(myword, your_word, guess1):
status = ''
matches = 0
for letter in myword:
if letter in your_word:
status += letter
else:
status += '*'
if letter == guess1:
matches += 1
if matches > 1:
print(matches, guess1)
elif matches == 1:
print(guess1)
return status
0.0000
0
2
关注作者
收藏
发表评论
暂无数据

