Smallest Number In An Array Python
Trying to find the smallest number in an array that the user inputs. Here's what I have: def main(): numbers = eval(input('Give me an array of numbers: ')) smallest = numbers
Solution 1:
You can just use min()
:
print("The smallest number is: ", min(numbers))
Solution 2:
You have to print the output only once after the loop finishes.
def main():
numbers = eval(input("Give me an array of numbers: "))
smallest = numbers[0]
for i in range(0,len(numbers),1):
if (numbers[i] < smallest):
smallest = numbers[i]
print("The smallest number is: ", smallest)
main()
Or use min()
as Christian suggested.
Post a Comment for "Smallest Number In An Array Python"