Monday, 15 March 2010

dictionary - Using the random function in Python -



dictionary - Using the random function in Python -

here function working , main code requires homecoming value:

def evilsetup(): """takes list of words of random length , places them dictionary based on position of chosen letter.randomly selects word key words.""" words = setup() result = {} char = input('please come in 1 letter guess: ') word in words: key = ' '.join(char if c == char else '-' c in word) if key not in result: result[key] = [] result[key].append(word) homecoming max(result.items(), key=lambda keyvalue: len(keyvalue[1])) collections import defaultdict import random words= evilsetup()#list of words take won, lost = 0,0 #accumulators games won, , lost while true: wrongs=0 # accumulator wrong guesses secretword = random.choice((words)[1]) print(secretword) #for testing purposes

what trying have secretword variable in main code store random word homecoming value in evilsetup(). previous post learned can homecoming random word in function straight using:

return random.choice(max(result.items(), key=lambda kv: len(kv[1]))[1])

i need same operation, have stored in secretword variable word chosen @ random each new game instead of keeping same word chosen evilsetup() function.

for reference , future utilize did figure out how this. using wrong syntax. here line of code works properly:

secretword = random.choice((words[1]))

it generates random word words corresponding dictionary key in first position.

python-3.x dictionary random

No comments:

Post a Comment