我对这个程序的最后一个功能,位置有意见。我必须计算列表中最大和最小数字的位置,但我不断得到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()
从不显式返回
position()
中的任何内容,因此None
被返回。然后它被迭代以分配给pos1
和pos2
。你知道吗相关问题 更多 >
编程相关推荐