使用while循环查找列表中的最大数字(python)

时间:2017-04-13 11:55:07

标签: python

我试过这段代码:

numbers = [1,2,5,8,4,99,3]

x = 0

while numbers[x+1] > numbers[x]: 
    x = x+1

print numbers[x]

输出为8

我怎么解决这个问题?

2 个答案:

答案 0 :(得分:0)

试试这个:

numbers = [1,2,5,8,4,99,3]

x = 0
lar = numbers[x]
while x < len(numbers):
  if numbers[x] > lar:
    lar = numbers[x]
  x = x+1
print lar

答案 1 :(得分:0)

a = [1,2,5,8,4,99,3]
x = 0 
y = 0

while y != len(a):
    if x < a[y]:
        x = a[y]
    y += 1

print x