我有一个生成随机单词的脚本,
import random
letters = ["a", "b", "c", "d","e", "f", "g", "h", "i", "j", "k", "l"]
def get_random_name(letters, length):
return ''.join(random.choice(letters) for i in range(length))
print(get_random_name(1,12))
但是当我跑步时,我收到了错误:
TypeError: object of type 'int' has no len()
请帮忙。哪里错了?
答案 0 :(得分:7)
您将字母作为int传递,您需要传递字母列表:
print(get_random_name(letters,12))
传递get_random_name(1,12)
您试图致电random.choice(1)
,这显然会失败