Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!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.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'variables': 0.07; '1000.': 0.09; 'solution,': 0.09; 'statements': 0.09; 'cc:addr:python- list': 0.11; '"if"': 0.16; '(3,': 0.16; '-tkc': 0.16; '1000):': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'ivan': 0.16; 'multiples': 0.16; 'pythonic': 0.16; 'range(0,': 0.16; 'subject:programming': 0.16; 'wrote:': 0.18; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'define': 0.26; 'header:In-Reply-To:1': 0.27; 'mod': 0.31; 'problem': 0.35; 'version': 0.36; 'charset:us-ascii': 0.36; 'rather': 0.38; 'easy': 0.60; 'new': 0.61; 'sum': 0.64; 'more': 0.64; 'total': 0.65; 'to:addr:gmail.com': 0.65; 'evaluate': 0.72; 'received:50.22': 0.84 Date: Mon, 21 Apr 2014 08:39:10 -0500 From: Tim Chase To: Ivan Ivanivich Subject: Re: symple programming task In-Reply-To: <7f496a70-6b85-4bc9-bf33-7aeaad813d11@googlegroups.com> References: <7f496a70-6b85-4bc9-bf33-7aeaad813d11@googlegroups.com> X-Mailer: Claws Mail 3.8.1 (GTK+ 2.24.10; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - boston.accountservergroup.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tim.thechases.com X-Get-Message-Sender-Via: boston.accountservergroup.com: authenticated_id: tim@thechases.com 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: 36 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1398087564 news.xs4all.nl 2880 [2001:888:2000:d::a6]:33897 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:70461 On 2014-04-21 06:21, Ivan Ivanivich wrote: > > Find the sum of all the multiples of 3 or 5 below 1000. > my new version of script: > > total = 0 > div1 = 3 > div2 = 5 > for basis in range(0, 1000): > mod = basis % div1 > if mod == 0: > total = total + basis > continue > mod = basis % div2 > if mod == 0: > total = total + basis > continue > > > > print("total = ", total) Now that you have a working solution, I don't mind giving my more pythonic solution: sum(dividend for dividend in range(1000) if any(dividend % divisor == 0 for divisor in (3, 5))) which succinctly states the problem and makes it easy to add/remove/change the divisors in one place rather than having to define multiple variables to hold them and "if" statements to evaluate them. -tkc