class Word():
def __init__(self, chosen_word=''):
self.chosen_word = chosen_word
def create_list_dict(self):
letter_list = []
letter_list = list(self.chosen_word)
print(letter_list)
letters_dict = {}
for letters in letter_list:
letters_dict['letter'] = letters
letters_dict['guessed'] = False
print(letters_dict)
complete_list = []
complete_list.append(letters_dict)
print(complete_list)
hello = Word('hello')
print(hello.create_list_dict()
currently have this code but it prints out separate dictionaries and lists for each of the letters and I want to create a list of dictionaries from each letter in a given word. Any suggestions?
CodePudding user response:
class Word():
def __init__(self, chosen_word=''):
self.chosen_word = chosen_word
def create_list_dict(self):
letter_list = []
letter_list = list(self.chosen_word)
#print(letter_list)
letter_dict = {}
complete_list = []
for letter in letter_list:
letter_dict = {f'letter {letter}': f'guessed {False}'}
complete_list.append(letter_dict)
return(complete_list)
hello = Word('hello')
print(hello.create_list_dict())
CodePudding user response:
I think Mark's solution is good. Getting to data is more simple with that solution :
Example getting the 2nd letter data :
complete_list = hello.create_list_dict()
print(complete_list[1]['letter'])
print(complete_list[1]['guessed'])
CodePudding user response:
Please upvote if the below solution works for you. Thanks
class Word():
def __init__(self, chosen_word=''):
self.chosen_word = chosen_word
def create_list_dict(self):
complete_list = []
letter_list = list(self.chosen_word)
letters_dict = {}
for letters in letter_list:
letters_dict['letter'] = letters
letters_dict['guessed'] = False
complete_list.append(letters_dict)
print complete_list
hello = Word('hello')
print(hello.create_list_dict())
Output :
[{'guessed': False, 'letter': 'o'}, {'guessed': False, 'letter': 'o'}, {'guessed': False, 'letter': 'o'}, {'guessed': False, 'letter': 'o'}, {'guessed': False, 'letter': 'o'}]