0
I need help to fix this code
list = [] word = “word” for letter in word: list = list + [letter] print (list) output——->[] must output—>[‘w’, ‘o‘ , ‘r’ , ‘d’]
3 ответов
+ 7
list = list + [letter]
is not working. use:
list.append(letter)
In general you should not use object names like list as names for variables and so on. if you would have followed this rule, your code would work:
list1 = []
word = "word"
for letter in word:
#list.append(letter)
list1 = list1 + [letter]
print (list1)
+ 1
Your code is correct. It outputs ['w', 'o', 'r', 'd'] as expected.
Run it again and let me know!
Edit: Lothar is correct about not using object names as variables.
+ 1
print([letter for letter in word])
print(list(word))
for letter in word:
lst.append(letter)
print(lst)
just some suggestions 😃