python计算列表中最大和最小元素的位置。非典型

2024-04-19 17:46:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我对这个程序的最后一个功能,位置有意见。我必须计算列表中最大和最小数字的位置,但我不断得到TypeError:“NoneType”对象不可iterable。我该怎么解决这个问题?谢谢。你知道吗

def main():
    data = getnumbers()
    top, bottom = calculate(data)
    pos1, pos2 = position(data, top, bottom)


    print("The numbers are,", data)
    print("The largest and lowest numbers are,", top, "and", bottom)
    print("The position of the largest number is,", pos1, "and the position of the smallest number is,", pos2, ".")


def getnumbers():
    nums = []
    xStr = input("Enter a number (<Enter> to quit) >> ")
    while xStr != "":
        x = eval(xStr)
        nums.append(x)   
        xStr = input("Enter a number (<Enter> to quit) >> ")
    return nums


def calculate(nums):
    top = max(nums)
    bottom = min(nums)
    return top, bottom

def position(nums, top, bottom):
    pos1 = nums.index(top)
    pos2 = nums.index(bottom)



main()

Tags: andthenumberdatatopdefpositionprint