Beginning Python: Find greatest number from function that gives input list of positive integers -


  1. new python, having trouble getting function display greatest number, reason have number display least. quiz using used code final solution, think wrong, appreciated.

    # define procedure, greatest, # takes input list # of positive numbers, , # returns greatest number # in list. if input # list empty, output # should 0. def greatest(list_of_numbers):     big = 0      in list_of_numbers:          if > big:              big =         return big   print greatest([4,23,1]) #>>> 23  can't 23 returns 4 reason.  print greatest([]) #>>> 0 

    for reason gives me 4 instead of 23 greatest.

you returning on first iteration. move return out 1 level:

def greatest(list_of_numbers):     big = 0      in list_of_numbers:          if > big:              big =     return big 

however entirely unnecessary python has built in:

def greatest(list_of_numbers):     return max(list_of_numbers)  

Comments