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

时间:2015-01-07 17:09:21

标签: python loops

我想获取一个数字的输入,并将其与具有两个起始值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

3 个答案:

答案 0 :(得分:1)

试试这个:

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>

答案 1 :(得分:1)

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

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]

答案 2 :(得分:0)

最终列出了短版本。

这部分混淆了实际的两个最大数字:

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类行为 通过说"好吧,我们有比这更大的东西,但这比右边的那个更大吗?" 。我们将排序留给插入过程结束。

所以我们得到了这个:

    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)