我是编程新手,我想将随机选择的单词附加在单独的字母列表(letterList)中
import random
wordList = ["bird", "dog", "cat", "school", "computer"]
underscoreList = []
letterList = []
word = random.choice(wordList)
for letter in word:
letterList.append[letter]
underscoreList.append["_"]
答案 0 :(得分:0)
您的代码基本上没问题,只是在想要括号(用于函数/方法调用)时滥用方括号(用于索引/查找)。 append
是list
上的一种方法,因此您需要使用括号来分隔参数,并进行以下更改:
letterList.append[letter]
underscoreList.append["_"]
收件人:
letterList.append(letter)
underscoreList.append("_")
提醒您,在这种情况下,您可以简化很多工作,只需简单地转换为list
和list
即可替换循环(以及list
的初始定义)分别相乘,只剩下:
import random
wordList = ["bird", "dog", "cat", "school", "computer"]
word = random.choice(wordList)
letterList = list(word) # Converts to list of characters directly
underscoreList = ['_'] * len(word) # Makes list of underscores of matching length
答案 1 :(得分:-1)
它是append(字母)而不是append(字母)
答案 2 :(得分:-1)
import random
wordList = ["bird", "dog", "cat", "school", "computer"]
underscoreList = []
letterList = []
for x in range(3):
word = random.choice(wordList)
letterList += [[x for x in word]]
for letter in word:
underscoreList.append("_")
print(letterList)