Path: csiph.com!usenet.pasdenom.info!news.franciliens.net!news.muarf.org!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'python,': 0.02; 'float': 0.07; 'modify': 0.07; 'string': 0.09; 'line:': 0.09; 'strings.': 0.09; 'subject:string': 0.09; 'worked.': 0.09; 'cc:addr:python- list': 0.11; 'jan': 0.12; 'question.': 0.14; "','": 0.16; '4.4,': 0.16; 'commas:': 0.16; 'did:': 0.16; 'separated': 0.16; 'wanted.': 0.16; 'wrote:': 0.18; 'split': 0.19; 'cc:addr:python.org': 0.22; 'print': 0.22; 'question': 0.24; 'cc:2**0': 0.24; 'script': 0.25; 'values': 0.27; 'header:In-Reply-To:1': 0.27; '----': 0.29; 'message-id:@mail.gmail.com': 0.30; 'url:mailman': 0.30; "skip:' 10": 0.31; 'decimal': 0.31; 'fine,': 0.31; 'subject:numbers': 0.31; 'url:python': 0.33; '-----': 0.33; 'subject:from': 0.34; 'problem': 0.35; 'subject:with': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'there': 0.35; 'url:listinfo': 0.36; 'url:org': 0.36; 'should': 0.36; 'january': 0.37; 'list': 0.37; 'starting': 0.37; 'thank': 0.38; 'pm,': 0.38; 'url:mail': 0.40; 'how': 0.40; 'solve': 0.60; 'new': 0.61; 'numbers': 0.61; 'first': 0.61; 'total': 0.65; 'different': 0.65; 'to:addr:gmail.com': 0.65; 'here': 0.66; '2015': 0.84; 'joel': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=hEFp6fLx46PMG9NolfdV1q+ZEn0l/ZFxExdx8WDc+TQ=; b=iaSuMb+c4DlvmQooqJtyB878OjycPaClK+6iSDzjpAQrgHzloo5ZnudSGEuUx9hCea we0FLOaXMh/si3znDppJRPnvWDec2ngSCbkCCSmGqp3T5EB1Oil72siHavIz3QMhKP1v 0jw+ABWwzM2reBIeZ6YDNI9CK95EDZH7uLxBTlUg+aJummn9Hh9kTUlx/wQ4aK3+1pao mHShC4u57Osa51e5zyEhc5XZ+K0Jfx45CLo1sAYTTXPsTnE19GMLpN4Iej3/DrHflh4o 5PDCviTRR1CO6rhqCQOf3i+abcumH2VviiWgVrbTCfSbelDayujFUJwKy7cfbLcymtsC gW/A== MIME-Version: 1.0 X-Received: by 10.140.20.226 with SMTP id 89mr43002349qgj.43.1421012577248; Sun, 11 Jan 2015 13:42:57 -0800 (PST) In-Reply-To: <3bfeea22-cded-4363-92fa-6f1b33ddae16@googlegroups.com> References: <7240a574-dfd7-46c3-80eb-0657b41894bb@googlegroups.com> <3bfeea22-cded-4363-92fa-6f1b33ddae16@googlegroups.com> Date: Sun, 11 Jan 2015 16:42:57 -0500 Subject: Re: extracting numbers with decimal places from a string From: Joel Goldstick To: Store Makhzan Content-Type: text/plain; charset=UTF-8 Cc: "python-list@python.org" X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 62 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1421012580 news.xs4all.nl 2930 [2001:888:2000:d::a6]:53629 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:83566 On Sun, Jan 11, 2015 at 3:26 PM, Store Makhzan wrote: > On Sunday, January 11, 2015 at 2:06:54 PM UTC-6, Joel Goldstick wrote: >> On Sun, Jan 11, 2015 at 2:31 PM, Store Makhzan wrote: >> > I have this script which can calculate the total of numbers given in a string >> > ---- script ----- >> > total = 0 >> > for c in '0123456789': >> > total += int(c) >> > print total >> > ---- script ----- >> > >> > How should I modify this script to find the total of if the numbers given in the string form have decimal places? That is, how do I need to modify this line: >> > ----- script ----- >> > for c in '1.32, 5.32, 4.4, 3.78': >> > ----- script ----- >> > to find the total of these given numbers. >> > -- >> > https://mail.python.org/mailman/listinfo/python-list >> >> split the string on ',' to get a list of strings. loop thru list >> using float(s), add float values >> >> >> -- >> Joel Goldstick >> http://joelgoldstick.com > > Thank you > Here is what I did: > > ----- script ----- > #Check if a perfect cube > total = 0 > for c in ('1.23', '2.4', '3.123'): > print float(c) > total += float(c) > print total > ----- script ----- > > Which gave me the result I wanted. > Since I am new to Python, I used () as a guess, and it worked. That's fine, but its different than your original question. In your original question you had a string of floats separated by commas. To solve that problem you need to first split the string on the commas: my_list = "1.23, 2.4, 3.123".split(",") that will give you ['1.23', '2.4', '3.123'] >From there do what you did starting with your for loop > -- > https://mail.python.org/mailman/listinfo/python-list -- Joel Goldstick http://joelgoldstick.com