Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: "Martin A. Brown" Newsgroups: comp.lang.python Subject: Re: Beginner Python Help Date: Fri, 18 Mar 2016 00:50:38 -0700 Lines: 98 Message-ID: References: <101edd27-17e1-497c-a60f-fa56b033563b@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII X-Trace: news.uni-berlin.de tfAda3HbWm97bLxNs+JR7AmrVswbi7PNLYx8FnSLbs9Q== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'subject:Python': 0.05; '(of': 0.07; '[0]': 0.07; 'assignment': 0.07; 'builtin': 0.07; 'cc:addr:python-list': 0.09; 'beginners': 0.09; 'posting.': 0.09; 'subject:Help': 0.10; 'python': 0.10; 'syntax': 0.13; 'useful,': 0.13; "'200": 0.16; '(integer)': 0.16; 'ah,': 0.16; 'already,': 0.16; 'code?': 0.16; 'dropping': 0.16; 'early.': 0.16; 'extraneous': 0.16; 'from:addr:martin': 0.16; 'list1': 0.16; 'luck,': 0.16; 'numbers;': 0.16; 'numerically': 0.16; 'parentheses': 0.16; 'received:172.18.18': 0.16; 'received:hsd1.or.comcast.net': 0.16; 'received:io': 0.16; 'received:or.comcast.net': 0.16; 'received:psf.io': 0.16; 'skip:> 20': 0.16; 'sorting': 0.16; 'sorts': 0.16; 'string:': 0.16; 'then?': 0.16; 'url:tutor': 0.16; 'later': 0.16; 'string': 0.17; 'integer': 0.18; 'stick': 0.18; 'string,': 0.18; 'input': 0.18; 'variable': 0.18; 'language': 0.19; '>>>': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; '(the': 0.22; 'martin': 0.22; 'produces': 0.22; 'trying': 0.22; 'cc:no real name:2**0': 0.22; 'header:In-Reply-To:1': 0.24; 'sort': 0.25; 'example': 0.26; '(e.g.': 0.27; '[2]': 0.27; 'have,': 0.27; 'function': 0.28; 'alan': 0.29; 'description,': 0.29; 'thinks': 0.29; 'convert': 0.29; 'there.': 0.30; 'url:mailman': 0.30; "i'd": 0.31; 'probably': 0.31; '[1]': 0.32; 'possibly': 0.32; 'useful': 0.33; 'problem': 0.33; 'url:python': 0.33; 'int': 0.33; 'received:comcast.net': 0.33; "i'll": 0.33; 'url:listinfo': 0.34; 'list': 0.34; 'done': 0.35; 'skip:> 10': 0.35; 'quite': 0.35; 'sometimes': 0.35; 'but': 0.36; 'too': 0.36; 'instead': 0.36; 'there': 0.36; 'url:org': 0.36; 'url:library': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'charset:us-ascii': 0.37; 'doing': 0.38; 'wrong': 0.38; 'several': 0.38; 'mailing': 0.38; 'does': 0.39; 'url:mail': 0.40; 'where': 0.40; 'called': 0.40; 'url:3': 0.60; 'your': 0.60; 'received:network': 0.61; 'determine': 0.61; 'back': 0.62; 'skip:n 10': 0.62; 'more': 0.63; 'different': 0.63; 'sample': 0.63; 'here:': 0.63; 'tutor': 0.66; 'therefore': 0.67; 'helping': 0.67; 'reply': 0.68; 'wish': 0.71; 'greetings': 0.71; 'prompt': 0.79; 'you:': 0.79; '>of': 0.84; 'complex,': 0.84; 'confusing': 0.84; 'devoted': 0.84; 'points,': 0.84; 'toy': 0.84; 'type(s)': 0.84; 'url:functions': 0.84; 'habit': 0.91; 'min': 0.91; 'numbers:': 0.91; 'start;': 0.91; 'quotation': 0.93 X-X-Sender: mabrown@dagger.wonderfrog.net In-Reply-To: <101edd27-17e1-497c-a60f-fa56b033563b@googlegroups.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:105188 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? # 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? # 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/