请问各位这个求列表最大值的问题出在哪呀?
本帖最后由 zltzlt 于 2020-4-18 13:11 编辑list01 =
list02 = list01
while True:
if list02 > list02:
del list02
else:
if list02 == list02:
del list02
else:
list02 = list02
print(list02)
你没有退出循环的条件,这样改改:
list01 =
list02 = list01
while len(list02)>1: #list2只剩下1个元素的时候退出
if list02 > list02:
del list02
else:
if list02 == list02:
del list02
else:
list02 = list02
print(list02) 哈哈哈,原来如此,谢谢老哥 你这是死循环
只需遍历一遍列表,把最大值存入一个变量中即可
list01 =
# list02 = list01
# while True:
# if list02 > list02:
# del list02
# else:
# if list02 == list02:
# del list02
# else:
# list02 = list02
# print(list02)
max_value = list01
for value in list01:
if value > max_value:
max_value = value
print(max_value) 循环没有退出 对list 删来删去的,,最后越界访问了呗 XiaoPaiShen 发表于 2020-4-18 07:43
你这是死循环
只需遍历一遍列表,把最大值存入一个变量中即可
谢谢老哥
页:
[1]