Python 为什么我的while命令不起作用?

Python 为什么我的while命令不起作用?,python,Python,我不明白为什么我的代码在打印“从8开始”时停止工作 为什么不把数字8和数字5,8,12,18,22进行比较 #Sum of two lowest integers numbers = [5,8,12,18,22] keep_ans = [] limit = len(numbers) for i in numbers: print("Start with "+str(i)) run = 0 check_in = 0 Done = False #It's stop h

我不明白为什么我的代码在打印“从8开始”时停止工作

为什么不把数字8和数字5,8,12,18,22进行比较

#Sum of two lowest integers
numbers = [5,8,12,18,22]
keep_ans = []
limit = len(numbers)
for i in numbers:
    print("Start with "+str(i))
    run = 0
    check_in = 0
    Done = False #It's stop here, when i = 8
    while Done == False:
        if i <= numbers[check_in]:
            print("Compare "+str(i)+" with "+str(numbers[check_in])+" round:"+str(run))
            run += 1
            check_in += 1
        if run == limit-1:
            keep_ans.append(i)
            Done = True
ans = sum(keep_ans)
print(ans)

您陷入了
的while
循环中,因为在第二次迭代中,您没有输入第一个
if
-条件,因此从未向
运行
签入中添加1

要解决此问题,您需要更改缩进:

while Done == False:
    if i <= numbers[check_in]:
        print("Compare "+str(i)+" with "+str(numbers[check_in])+" round:"+str(run))
    run += 1
    check_in += 1
完成时==False:

如果我我得到了我的答案!,多谢各位

numbers = [5,8,12,18,22]
keep_ans = []
print("Number => "+str(numbers))
for i in numbers:
    print("Start with  >  "+str(i)+"  <")
    point = 0
    round = 0
    limit = len(numbers)
    for i2 in numbers:
        print("     Compare with :"+str(numbers[point]))
        if i < i2:
            round += 1
            #print("     get")
        point += 1
        if round == limit-2:
            #print("Added!!!!!")
            keep_ans.append(i)
            break
    #print(keep_ans)
print("Two lowest numbers = "+str(keep_ans))
print("Sum of two lowest numbers = "+str(sum(keep_ans)))

### Output ###
Number => [5, 8, 12, 18, 22]
Start with  >  5  <
     Compare with :5
     Compare with :8
     Compare with :12
     Compare with :18
     Compare with :22
Start with  >  8  <
     Compare with :5
     Compare with :8
     Compare with :12
     Compare with :18
     Compare with :22
Start with  >  12  <
     Compare with :5
     Compare with :8
     Compare with :12
     Compare with :18
     Compare with :22
Start with  >  18  <
     Compare with :5
     Compare with :8
     Compare with :12
     Compare with :18
     Compare with :22
Start with  >  22  <
     Compare with :5
     Compare with :8
     Compare with :12
     Compare with :18
     Compare with :22
Two lowest numbers = [5, 8]
Sum of two lowest numbers = 13
number=[5,8,12,18,22]
保持一致性=[]
打印(“数字=>”+str(数字))
以数字表示的i:

打印(“开始>”+STR(i)+“考虑在您的代码中发生了什么”,而<>代码>循环,如果它内部的条件都没有被评估为真。
numbers = [5,8,12,18,22]
keep_ans = []
print("Number => "+str(numbers))
for i in numbers:
    print("Start with  >  "+str(i)+"  <")
    point = 0
    round = 0
    limit = len(numbers)
    for i2 in numbers:
        print("     Compare with :"+str(numbers[point]))
        if i < i2:
            round += 1
            #print("     get")
        point += 1
        if round == limit-2:
            #print("Added!!!!!")
            keep_ans.append(i)
            break
    #print(keep_ans)
print("Two lowest numbers = "+str(keep_ans))
print("Sum of two lowest numbers = "+str(sum(keep_ans)))

### Output ###
Number => [5, 8, 12, 18, 22]
Start with  >  5  <
     Compare with :5
     Compare with :8
     Compare with :12
     Compare with :18
     Compare with :22
Start with  >  8  <
     Compare with :5
     Compare with :8
     Compare with :12
     Compare with :18
     Compare with :22
Start with  >  12  <
     Compare with :5
     Compare with :8
     Compare with :12
     Compare with :18
     Compare with :22
Start with  >  18  <
     Compare with :5
     Compare with :8
     Compare with :12
     Compare with :18
     Compare with :22
Start with  >  22  <
     Compare with :5
     Compare with :8
     Compare with :12
     Compare with :18
     Compare with :22
Two lowest numbers = [5, 8]
Sum of two lowest numbers = 13