如何将名称存储在列表中,以便当我输入新名称时,我输入的旧名称会保留?

时间:2016-12-13 18:05:32

标签: python-3.5

def valid_cond1(self):
    # err_df = rows of self.df not meeting cond1
    bad_lines = []
    for ix, val in err_df.iterrows():
        bad_lines.append("Error in line %s: %s.  Cond1 not met.." % (ix,val))
    return bad_lines

1 个答案:

答案 0 :(得分:0)

您正在while循环的每一步重新初始化两个列表goodListbadList。 为避免这种情况,请在while循环之前(之前)初始化它们:

goodList = ["Max","James"]
badList = ["Bradley"]

while True:
    childName = input("What name is it?")
    childBehaviour = input("Have they been bad or good?")
    answer = (childName)
    if childBehaviour == 'good':
        goodList.append(answer)
        print('GoodList' + str(goodList))
    if childBehaviour == 'bad':
        badList.append(answer)
        print('BadList' + str(badList))