Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed1a.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.008 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'counting': 0.09; 'moreover,': 0.09; 'polish': 0.09; '\xe2\x80\x94': 0.09; 'cc:addr :python-list': 0.11; 'python': 0.11; 'jan': 0.12; '3:27': 0.16; 'iteration': 0.16; 'iteration.': 0.16; 'language': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'solution.': 0.20; 'cc:addr:python.org': 0.22; 'error': 0.23; 'subject:problem': 0.24; 'mon,': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'compare': 0.26; 'skip:" 20': 0.27; 'header:In-Reply-To:1': 0.27; 'correct': 0.29; 'chris': 0.29; 'see,': 0.30; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; "skip:' 10": 0.31; 'comparison': 0.31; 'everyone': 0.33; 'checking': 0.33; 'subject:with': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'should': 0.36; 'pm,': 0.38; 'letters': 0.60; 'first': 0.61; 'here:': 0.62; 'to:addr:gmail.com': 0.65; 'you:': 0.81; '2015': 0.84; 'off,': 0.84 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:content-transfer-encoding; bh=MiohRNOy/VC3raK7ZK6Kl5+y0UJN78IswoTFkhWqKcE=; b=IHvvSK9eH5AbGTdjD+qf4roDmOdZXx2/pZemxbadDQgOpY9csFhQ7lD7LTMJlcyE8W Sp1U4uadbtqr+oUvFVUfGC1WulHTIVNbAml5ObuaQ2RPQtJ/o7iJhFgg2ldjO3HUxcX6 Sk26aS+GBzDSpEd59cVXlpNJlt98aSciUpu/4qPnzPUlkpkmb7kV7IpZa+mSfv1Zga7k s2qX0xHgEnyWMh3DqTV5UewW4e+HVOidkRp/a6jfmZyILXktNvxWOwJ4f+BZGiz3mVy2 tVD3KQKH0CQTpKatG0GomS+gkfFV+aQQnHgSWs0TOzYx+QKiXKfppuEUDpjbNHXALExG fuLg== MIME-Version: 1.0 X-Received: by 10.152.206.108 with SMTP id ln12mr92636997lac.3.1420468885134; Mon, 05 Jan 2015 06:41:25 -0800 (PST) In-Reply-To: References: Date: Mon, 5 Jan 2015 15:41:25 +0100 Subject: Re: problem with for and if From: Chris Warrick To: Dariusz Mysior Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable 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: 35 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1420468894 news.xs4all.nl 2947 [2001:888:2000:d::a6]:50959 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:83245 On Mon, Jan 5, 2015 at 3:27 PM, Dariusz Mysior wro= te: > I want search count of szukana in zmienna but code below counting all 12 = letters from "traktorzysta" word First off, I recommend that you do not write code in Polish =E2=80=94 use English so that everyone can understand your code. Your error is here: > for a in zmienna: > if szukana in zmienna: You shouldn=E2=80=99t be looking in `zmienna`, you should be checking `a` = =E2=80=94 the letter of the word used in the current iteration. Moreover, you should use =3D=3D for the comparison =E2=80=94 compare the letter in the cu= rrent iteration to 't'. The correct line WOULD be if a =3D=3D szukana: BUT there is actually a better solution. You see, Python is a language with =E2=80=9Cbatteries included=E2=80=9D =E2=80=94 that is, many = things are already available for you to use without need to re-implement them. Just use str.count, which will do it for you: zmienna =3D 'traktorzysta' szukana =3D 't' print("Literka '",szukana,"' w s=C5=82owie ",zmienna, "wyst=C4=85pi=C5=82a ",zmienna.count(szukana)," razy") --=20 Chris Warrick PGP: 5EAAEA16