我有这样的功能
def party_generator(size=1, chars=string.ascii_uppercase):
parties = []
for y in range(2):
party = ''.join(random.choice(chars) for x in range(size))
parties.append(''.join(['Party ',party]))
return parties
执行它给了我
>>> parties = party_generator()
>>> parties
['Party Z']
如何让它返回包含2个成员的列表?
答案 0 :(得分:2)
您的缩进将return
置于 for
循环中,因此它会在第一次迭代时发生。移出来:
def party_generator(size=1, chars=string.ascii_uppercase):
parties = []
for y in range(2):
party = ''.join(random.choice(chars) for x in range(size))
parties.append(''.join(['Party ',party]))
return parties
缩进在Python中很重要!