January 24th, 2013, 09:24 PM

How to get the largest and smallest numbers in a list
I've been learning python for a few months now and can create a few games. Now I need this really simple thing and I'm embarrassed to ask about it. I need to find the largest and smallest numbers in a list.
I've googled and tried all the suggestions but still none of them work. If I put in numbers like '100000', '100', '10'. It will say 10 is the biggest.
Here's my code, any help would be great:
Code:
numbers = []
user_input = 0
while user_input != "q":
user_input = input("Enter a number: ")
if user_input == "q":
break
else:
numbers.append(user_input)
#print out the numbers
for i in numbers:
print(i, end=" ")
#the amount of numbers
print("The amount of numbers is: {0}".format(str(len(numbers))))
#print the sum of all the numbers
total = 0
for i in numbers:
total += int(i)
print("The total amount is {0}".format(str(total)))
#print the lowest and highest numbers
numbers.sort()
print("The lowest number is {0}".format(numbers[1:][0]))
print("The highest number is {0}".format(max(numbers)))
#the mean of all the numbers
mean = total / len(numbers)
print("The mean number is {0}".format(str(mean)))
January 24th, 2013, 09:45 PM

I don't know what your code is trying to do for finding the lowest number. Changing it to simply calling min(number) caused it to work fine for me. Well, but there are other problems: namely, why are you storing your numbers as strings? If it's because of the use of string formatting, you may be interested to know that it is quite possible to insert numbers into formatted strings.
Here is my altered version which stores the numbers as ints:
Code:
numbers = []
user_input = 0
while user_input != "q":
user_input = input("Enter a number: ")
if user_input != "q":
#There's no reason to break when it equals "q" since this is the
#end of the loop anyway.
numbers.append(int(user_input))
#print out the numbers
for i in numbers:
print(i, end=" ")
#the amount of numbers
print("The amount of numbers is: {0:d}".format(len(numbers)))
#print the sum of all the numbers
total = 0
for i in numbers:
total += i
print("The total amount is {0:d}".format(total))
#print the lowest and highest numbers
numbers.sort()
print("The lowest number is {0:d}".format(min(numbers)))
print("The highest number is {0:d}".format(max(numbers)))
#the mean of all the numbers
mean = total / len(numbers)
print("The mean number is {0:G}".format(mean))
January 24th, 2013, 09:47 PM

You realized for summing the numbers you'd need to convert from string to a numeric type. Here I've converted the strings to numbers before enlisting them.
Code:
# python3
numbers = []
while True:
user_input = input("Enter a number: ")
if user_input == "q":
break
try:
number = float(user_input)
except:
print('bad data omitted. q terminates entry')
else:
numbers.append(number)
#print out the numbers
for i in numbers:
print(i, end=" ")
#the tally of numbers
print("The tally of numbers is: {0}".format(str(len(numbers))))
#print the sum of all the numbers
total = sum(numbers)
print("The total amount is {0}".format(str(total)))
#print the lowest and highest numbers
print("The lowest number is {0}".format(min(numbers)))
print("The highest number is {0}".format(max(numbers)))
#the mean of all the numbers
mean = total / len(numbers)
print("The mean number is {0}".format(str(mean)))
Last edited by b49P23TIvg; January 24th, 2013 at 09:49 PM.
[code]
Code tags[/code] are essential for python code and Makefiles!
January 25th, 2013, 01:31 PM

Thanks guys.
I got too far ahead of myself and began to forget the basics of the language!