#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2010
    Posts
    18
    Rep 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. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    114
    Rep Power
    4
    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))
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,995
    Rep Power
    481
    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 10:49 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2010
    Posts
    18
    Rep Power
    0
    Thanks guys.

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

IMN logo majestic logo threadwatch logo seochat tools logo