python 数组中的第一个条目未被捕获

python first entry in array not being captured

我想输入一个数字,并将其与具有两个起始值 0 和 0 的列表进行比较。如果数字大于 list[0],请将 list[0] 替换为数字. list[1] 也这样做。如果输入负数,则结束循环并打印包含新数字的列表。如果在负数之前输入的正数少于两个,则打印一条说明。

我也想让电脑显示新的列表。

问题是这样的,我输入的第一个数字被代码忽略了,没有被捕获到列表中,我不确定为什么。我包含了我的代码以及运行以下代码的结果。

def printTwoLargest():
    number = eval(input('Please enter a number'))
    largest = [0,0]
    while number > -1:
        if number > largest[0]:
            largest[1] = largest[0]
            print (largest) #remove after debug
            largest[0] = number
        elif number > largest[1]:
            print (largest) # remove after debug
            largest[1] = number
        number = eval(input('Please enter a  number'))
    if largest[0] == 0 or largest [1] == 0:
        print('You have not entered enough positive numbers, please enter at least two positive numbers')
    else:
        print (largest)

## print statements are a debug test. Cant figure out why, but it seems largest[0]
## and largest[1] are not filled until the 3rd postive number input. It should be
## the second one. 

和shell运行后的结果:

>>> printTwoLargest()
Please enter a number 5
[0, 0]
Please enter a  number 4
[5, 0]
Please enter a  number 6
[5, 5]
Please enter a  number -1
[6, 5]
>>> printTwoLargest()
Please enter a number 9
[0, 0]
Please enter a  number 8
[9, 0]
Please enter a  number -1
You have not entered enough positive numbers, please enter at least two positive numbers
>>> printTwoLargest()
Please enter a number -1
You have not entered enough positive numbers, please enter at least two positive numbers

试试这个:

def printTwoLargest():
    number = int(input('Please enter a number'))
    largest = [0,0]
    while number > -1:
        if number > largest[0]:
            largest[1] = largest[0]
            largest[0] = number
            print (largest) #remove after debug
        elif number > largest[1]:
            largest[1] = number
            print (largest) # remove after debug
        number = int(input('Please enter a  number'))
    if largest[0] == 0 or largest[1] == 0:
        print('You have not entered enough positive numbers, please enter at least two positive numbers')
    else:
        print (largest)

输出:

C:\Users\so>C:\Python27\python.exe ptl.py
Please enter a number6
[6, 0]
Please enter a  number4
[6, 4]
Please enter a  number-1
[6, 4]

C:\Users\so>

最后列出了简短版本。

这部分弄乱了实际的两个最大数字:

if number > largest[0]:
    # you're losing largest[1] here
    # but it may not be smaller than largest[0] (example: [4,9] -> [number, 4]) 
    largest[1] = largest[0]
    largest[0] = number
elif number > largest[1]:
    print (largest) # remove after debug
    largest[1] = number

一种解决它并保持代码与现有代码相似的方法(它可以进一步改进,但需要更多编辑)- 使用类似 carry 的行为 通过说 "OK, we have something bigger than this one, but is this bigger than the one on the right?"。我们省略了插入过程结束时的排序。

所以我们得到这个:

    if number > largest[0]:
        carry = largest[0]
        largest[0] = number
        # compare carried number to next number, remove if needed
        if carry > largest[1]:
             largest[1] = carry
    elif number > largest[1]:
        largest[1] = number
    number = eval(input('Please enter a  number'))

    # sort the numbers
    largest = sorted(largest)

    if largest[0] == 0 or largest [1] == 0:
        print('You have not entered enough positive numbers, please enter at least two positive numbers')
    else:
        print (largest)

以及整个代码:

def printTwoLargest():
    number = int(input('Please enter a number'))
    largest = [0,0]

    while number > -1:

        if number > largest[0]:
            carry = largest[0]
            largest[0] = number
            # compare carried number to next number, remove if needed
            if carry > largest[1]:
                largest[1] = carry
        elif number > largest[1]:
             largest[1] = number
        print(largest)
        number = eval(input('Please enter a  number'))

    # sort the numbers
    largest = sorted(largest)

    if largest[0] == 0 or largest [1] == 0:
        print('You have not entered enough positive numbers, please enter at least two positive numbers')
    else:
        print (largest)

如果您需要短版:

def printTwoLargest():
    largest = [0,0]
    while True:
        number = eval(input('Please enter a  number'))
        if number < 0:
            break
        largest = sorted(largest + [number])[-2:]

    if 0 in largest:
        print('You have not entered enough positive numbers, please enter at least two positive numbers')
    else:
        print (largest)

我想我知道你想要的行为并已将代码调整为以下内容:

def printTwoLargest():
    largest = [0,0]
    print largest
    number = int(input('Please enter a number\n'))
    while number > -1:
        if number > largest[1]:
            largest[0] = largest[1]
            largest[1] = number
        elif number > largest[0]:
            largest[0] = number
        print largest
        number = int(input('Please enter a  number'))
    if largest[0] == 0 or largest [1] == 0:
        print('You have not entered enough positive numbers, please enter at least two positive numbers')
    else:
        print (largest)

此代码所做的是获取新数字并首先与最高数字(右边的数字或索引 1)进行比较。如果它不高于那个值,它会检查较低的值(索引 0)。这只会一次更新 1 个。这是它在一些输入后的样子

In [3]: printTwoLargest()
[0, 0]
Please enter a number
1
[0, 1]
Please enter a  number5
[1, 5]
Please enter a  number6
[5, 6]
Please enter a  number-1
[5, 6]