Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
January 24th, 2013, 10:24 PM
 RexT
Registered User

Join Date: Sep 2010
Posts: 18
Time spent in forums: 5 h 28 m 39 sec
Reputation Power: 0
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)))```

#2
January 24th, 2013, 10:45 PM
 Nyktos
Contributing User

Join Date: Dec 2012
Posts: 114
Time spent in forums: 1 Day 11 h 54 m 6 sec
Reputation Power: 3
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))```

#3
January 24th, 2013, 10:47 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,132
Time spent in forums: 1 Month 3 Weeks 2 Days 5 h 47 m 38 sec
Reputation Power: 455
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)))```
__________________
[code]Code tags[/code] are essential for python code!

Last edited by b49P23TIvg : January 24th, 2013 at 10:49 PM.

#4
January 25th, 2013, 02:31 PM
 RexT
Registered User

Join Date: Sep 2010
Posts: 18
Time spent in forums: 5 h 28 m 39 sec
Reputation Power: 0
Thanks guys.

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

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > How to get the largest and smallest numbers in a list