Trying to make a simple function that determines the smallest number in a list of numbers. For some reason, it looks like my for loop is only executing once for some reason with this function. What am I missing?
def smallestnum(list):
smallest = None
for value in list:
if smallest is None:
smallest = value
elif value < smallest :
smallest = value
return smallest
mylist = [41, 12, 3, 74, 15]
x = smallestnum(mylist)
print(x)
Advertisement
Answer
Your return statement is in your for loop. Remove the last indent of your return, and it should work fine.
def smallestnum(list):
smallest = None
for value in list:
if smallest is None:
smallest = value
elif value < smallest :
smallest = value
return smallest