簡體   English   中英

python計算列表中最大和最小元素的位置。 無類型錯誤

[英]python calculate the position of the largest and smallest elements of the list. Nonetype error

我對該程序的最后一個功能位置有疑問。 我必須計算列表中最大和最小數字的位置,但我不斷收到TypeError:“ NoneType”對象不可迭代。 我該如何解決? 謝謝。

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()

您永遠不會從position()顯式返回任何內容,因此將返回None 然后對其進行迭代以分配給pos1pos2

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM