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?
JavaScript
x
14
14
1
def smallestnum(list):
2
smallest = None
3
for value in list:
4
if smallest is None:
5
smallest = value
6
elif value < smallest :
7
smallest = value
8
return smallest
9
10
mylist = [41, 12, 3, 74, 15]
11
x = smallestnum(mylist)
12
print(x)
13
14
Advertisement
Answer
Your return
statement is in your for
loop. Remove the last indent of your return
, and it should work fine.
JavaScript
1
9
1
def smallestnum(list):
2
smallest = None
3
for value in list:
4
if smallest is None:
5
smallest = value
6
elif value < smallest :
7
smallest = value
8
return smallest
9