import random
avchars = ['a','b','c','d','e','f','g']
ra = random.randint(8,24)
rb = random.randint(1,7)
for i in range(ra):
file = open("pass","w")
ac = avchars(rb)
print(ac)
file.write(ac)
file.close()
print("Done")
我想从上面的列表中生成8-24个字符。但是,我收到错误:
TypeError:' list'对象不可调用
答案 0 :(得分:2)
较短的版本:
with open('pass', 'w') as fobj:
fobj.write(''.join(random.choice('abcedfg') for _ in
range(random.randint(8, 24))))
使用过的功能:
random.choice(seq)
从非空序列中选择一个随机元素。
和
random.randint(a, b)
返回范围[a,b]中的随机整数,包括两个端点。
答案 1 :(得分:1)
列表不是元组或序列。因此你不能使用:
avchars(rc)
改为使用:
avchars[rc]
然后你的代码不会抛出错误。