Reputation: 33
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)
Upvotes: 0
Views: 355
Reputation: 328
It seems your return
statement is within the for loop so it gets executed at the end of the first pass.
Removing the return
from the for loop will solve your problem.
Upvotes: 2
Reputation:
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
Upvotes: 4