为什么我的变量在每个循环中都无缘无故地改变?

问题描述 投票:0回答:0

为什么我的变量在每个循环中都无缘无故地改变?

我正在使用 leetcode 进行训练,我发现了一个奇怪的问题。

有我的代码:

def kidsWithCandies(candies, extraCandies: int):
    output = []
    for enfant in range(len(candies)) :
        print(candies) #Show  candies for debug
        test_candies = candies
        test_candies[enfant] = test_candies[enfant]+extraCandies
        is_great = True
        for enfant2 in range(len(test_candies)) :
            if enfant == enfant2 :
                continue
            if test_candies[enfant] < test_candies[enfant2] :
                is_great = False
        output.append(is_great)
    return output
print(kidsWithCandies([2,3,5,1,3],3))

我得到这个输出:

[2, 3, 5, 1, 3]
[5, 3, 5, 1, 3]
[5, 6, 5, 1, 3]
[5, 6, 8, 1, 3]
[5, 6, 8, 4, 3]
[True, True, True, False, False]

奇怪的是,“糖果”在每个循环中都是不同的,没有明显的原因。

我不明白什么吗?

python python-3.x
© www.soinside.com 2019 - 2024. All rights reserved.