所以我试图为练习制作一个刽子手游戏。我已经编写了函数来获取随机单词和一个函数来将这些字符与索引配对。想知道用户是否猜测了正确的字符是否有引用字典的方法,并将字符输出到正确索引的空列表? Code I到目前为止:
import random
import sys
def hangman():
print("Welcome to Hangman\n")
answer = input("Would you like to play? yes(y) or no(n)")
if answer is "y":
print("Generating Random Word...Complete")
wordlist = []
with open('sowpods.txt', 'r') as f:
line = f.read()
line = list(line.splitlines())
word = list(random.choice(line))
Key = matchUp(word)
else:
sys.exit()
def matchUp(word):
list = []
for x in word:
list.append(word.index(x))
newDict = {}
newDict = dict(zip(word, list))
return newDict
hangman()
答案 0 :(得分:1)
这样吗?你可以跳过整个字典......
a = "_" * len(word)
def letter_check(letter):
if letter in word:
a[word.index(letter)] = letter
# possibly print 'a' here
else:
# function for decrement tries here
编辑:哎呀...我忘了可能重复的信件......嗯......这个怎么样:
a = "_" * len(word)
def letter_check(letter):
if letter in word:
for x, y in enumerate(word):
if letter == y:
a[x] = letter
else:
# function for decrement tries here