我对python很新。我正在研究随机密码生成器。我能够让我的程序生成一个
随机密码。但是它中有逗号(,
)。我想删除逗号。
看起来我的strip命令出错了。下面的程序产生输出:
AttributeError: 'list' object has no attribute 'strip'
#! /usr/bin/python
import random
import string
def letter_num():
letter = random.choice(string.letters)
number = random.randint(0,9)
letter_number = [letter, number]
random_letnum = random.choice(letter_number)
return random_letnum
#print letter_num(),letter_num(),letter_num(),letter_num(),letter_num(),letter_num,letter_num(),letter_num()
password = [letter_num(),letter_num(),letter_num(),letter_num(),letter_num(),letter_num(),letter_num(),letter_num()]
print password.strip(",")
答案 0 :(得分:0)
这是因为在您的代码中,密码是一个列表,您不能在列表中使用条带。
尝试以下代码
#! /usr/bin/python
import random
import string
def letter_num():
letter = random.choice(string.letters)
number = random.randint(0,9)
letter_number = [letter, number]
random_letnum = random.choice(letter_number)
return random_letnum
password = ""
for a in xrange(10):
password += str(letter_num())
print password.strip(",")