2 回答
TA贡献1875条经验 获得超5个赞
random.sample(word_list, 1)返回list像 ["sparkle"] 这样的单个项目,并且您将其转换为str,因此它[""]在单词周围添加,因此单词变为'["sparkle"]'
我认为你需要:
import random
word_list = ['tiger','dog','cake','disney','donut','rose','clouds','movies','sparkle','yoga','walrus','candle','mango','taco','flowers']
word = random.sample(word_list, 1)
word_len = len(word[0])
print(word, word_len)
输出:
['sparkle'] 7
TA贡献1860条经验 获得超8个赞
你使用了错误的方法
import random
word_list = ['tiger','dog','cake','disney','donut','rose','clouds','movies','sparkle','yoga','walrus','candle','mango','taco','flowers']
# return a k length list of unique elements chosen from the population sequence or set
# where k is 1, population sequence is word_list
print(random.sample(word_list, 1))
# to get random item from list you have to use random.choice
print(random.choice(word_list))
添加回答
举报