find the maximum number in a list using a loop

后端 未结 10 1586
野趣味
野趣味 2021-01-02 23:16

So I have this list and variables:

nums = [14, 8, 9, 16, 3, 11, 5]

big = nums[0]

spot = 0

I\'m confused on how to actually do it. Please

10条回答
  •  庸人自扰
    2021-01-03 00:03

    Python already has built in function for this kind of requirement.

    list = [3,8,2,9]
    max_number = max(list)
    print max_number # it will print 9 as big number
    

    however if you find the max number with the classic vay you can use loops.

    list = [3,8,2,9]
    
    current_max_number = list[0]
    for number in list:
        if number>current_max_number:
            current_max_number = number
    
    print current_max_number #it will display 9 as big number
    

提交回复
热议问题