我在尝试使用函数以计算列表的最小值,最大值和总和时遇到了一些麻烦。 该列表是随机生成的,用户可以选择列表中存在的值。 有人可以帮我弄清楚如何使用tempList和tempdim(临时维度)创建函数吗?
我有一个代码来制作尺寸和随机列表,我觉得我有一个非常好的程序结构..我只是无法弄清楚如何制作功能代码。
这就是我到目前为止......
import random
MINRAND = 1
MAXRAND = 1000
def main():
dimensions = int(input("How large do you want your square 2D array? "))
numbers = [[] for i in range(dimensions)]
generateList(numbers, dimensions)
printList(numbers, dimensions)
print("2-D List Highest Element is: ", findHighest(tempList))
print("2-D List Lowest Element is: ", findLowest(tempList))
print("2-D List Sum is: ", sum2Dlist(tempList))
def findHighest(tempList, tempdim):
def findLowest(tempList, tempdim):
def sum2Dlist(tempList, tempdim):
def generateList(tempList, tempdim):
for row in range(tempdim):
for col in range(tempdim):
tempList[row].append(random.randint(MINRAND, MAXRAND))
def printList(templist, tempdim):
for row in range(tempdim):
for col in range(tempdim):
print(format(templist[row][col], '6d'), end=" ")
print()
# Call the main function
main()

非常感谢!!