【发布时间】:2015-01-09 22:31:59
【问题描述】:
这是我的功能:
def evilSetup():
words = setUp()
result = {}
char = input('Please enter your one letter guess: ')
for word in words:
key = ' '.join(char if c == char else '-' for c in word)
if key not in result:
result[key] = []
result[key].append(word)
return max(result.items(), key=lambda keyValue: len(keyValue[1]))
from collections import defaultdict
import random
words= evilSetup()#list of words from which to choose
won, lost = 0,0 #accumulators for games won, and lost
while True:
wrongs=0 # accumulator for wrong guesses
secretWord = words
print(secretWord) #for testing purposes
guess= len(secretWord)*'_'
print('Secret Word:' + ' '.join(guess))
while wrongs < 8 and guess != secretWord:
wrongs, guess = playRound(wrongs, guess)
won, lost = endRound(wrongs,won,lost)
if askIfMore()== 'N':
break
printStats(won, lost)
该函数将获取单词列表,并根据猜测字母的位置将它们排序到字典中。截至目前,它返回最大的键值对。我希望它最终返回的是最大字典条目中的一个随机单词。截至目前,这些值采用列表的形式。
例如 {- - -, ['aah', 'aal', 'aas']}
理想情况下,我会从该列表中随机获取一个单词以返回。任何帮助表示赞赏。提前致谢。
【问题讨论】:
标签: python-3.x dictionary random