Greetings Alan and welcome to Python, >I just started out python and I was doing a activity where im >trying to find the max and min of a list of numbers i inputted. > >This is my code.. > >num=input("Enter list of numbers") >list1=(num.split()) > >maxim= (max(list1)) >minim= (min(list1)) > >print(minim, maxim) > >So the problem is that when I enter numbers with an uneven amount >of digits (e.g. I enter 400 20 36 85 100) I do not get 400 as the >maximum nor 20 as the minimum. What have I done wrong in the code?
I will make a few points, as will probably a few others who read your posting. * [to answer your question] the builtin function called input [0] returns a string, but you are trying to get the min() and max() of numbers; therefore you must convert your strings to numbers You can determine if Python thinks the variable is a string or a number in two ways (the interactive prompt is a good place to toy with these things). Let's look at a string: >>> s = '200 elephants' >>> type(s) # what type is s? <class 'str'> # oh! it's a string >>> s # what's in s? '200 elephants' # value in quotation marks! The quotation marks are your clue that this is a string, not a number; in addition to seeing the type. OK, so what about a number, then? (Of course, there are different kinds of numbers, complex, real, float...but I'll stick with an integer here.) >>> n = 42 >>> type(n) # what type is n? <class 'int'> # ah, it's an int (integer) >>> n # what's in n? 42 # the value * Now, perhaps clearer? max(['400', '20', '36', '85', '100']) is sorting your list of strings lexicographically instead of numerically (as numbers); in the same way that the string 'rabbit' sorts later than 'elephant', so too does '85' sort later than '400' * it is not illegal syntax to use parentheses as you have, but you are using too many in your assignment lines; I'd recommend dropping that habit before you start; learn when parentheses are useful (creating tuples, calling functions, clarifying precedence); do not use them here: list1 = (num.split()) # -- extraneous and possibly confusing list1 = num.split() # -- just right * also, there is also Tutor mailing list [1] devoted to helping with Python language acquisition (discussions on this main list can sometimes be more involved than many beginners wish to read) I notice that you received several answers already, but I'll finish this reply and put your sample program back together for you: num = input("Enter list of numbers: ") list1 = list(map(int, num.split())) print(list1) maxim = max(list1) minim = min(list1) print(minim, maxim) You may notice that map [2] function in there. If you don't understand it, after reading the function description, I'd give you this example for loop that produces the same outcome. list1 = list() for n in num.split(): list1.append(int(n)) The map function is quite useful, so it's a good one to learn early. Good luck, -Martin [0] https://docs.python.org/3/library/functions.html#input [1] https://mail.python.org/mailman/listinfo/tutor/ [2] https://docs.python.org/3/library/functions.html#map -- Martin A. Brown http://linux-ip.net/ -- https://mail.python.org/mailman/listinfo/python-list