Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #83245

Re: problem with for and if

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 <kwpolska@gmail.com>
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 <a71e126d-4b27-42a3-8103-3381b62794d2@googlegroups.com>
References <a71e126d-4b27-42a3-8103-3381b62794d2@googlegroups.com>
Date Mon, 5 Jan 2015 15:41:25 +0100
Subject Re: problem with for and if
From Chris Warrick <kwpolska@gmail.com>
To Dariusz Mysior <mysior.darek@gmail.com>
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 <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.17397.1420468894.18130.python-list@python.org> (permalink)
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

Show key headers only | View raw


On Mon, Jan 5, 2015 at 3:27 PM, Dariusz Mysior <mysior.darek@gmail.com> wrote:
> 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 — use
English so that everyone can understand your code.

Your error is here:
>     for a in zmienna:
>         if szukana in zmienna:

You shouldn’t be looking in `zmienna`, you should be checking `a` —
the letter of the word used in the current iteration.  Moreover, you
should use == for the comparison — compare the letter in the current
iteration to 't'.  The correct line WOULD be

    if a == szukana:

BUT there is actually a better solution.  You see, Python is a
language with “batteries included” — 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 = 'traktorzysta'
szukana = 't'
print("Literka '",szukana,"' w słowie ",zmienna,
      "wystąpiła ",zmienna.count(szukana)," razy")

-- 
Chris Warrick <https://chriswarrick.com/>
PGP: 5EAAEA16

Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

problem with for and if Dariusz Mysior <mysior.darek@gmail.com> - 2015-01-05 06:27 -0800
  Re: problem with for and if Shiyao Ma <i@introo.me> - 2015-01-05 22:38 +0800
  Re: problem with for and if YBM <ybmess@nooos.fr.invalid> - 2015-01-05 15:39 +0100
  Re: problem with for and if Chris Warrick <kwpolska@gmail.com> - 2015-01-05 15:41 +0100
  Re: problem with for and if Dariusz Mysior <mysior.darek@gmail.com> - 2015-01-05 06:47 -0800
  Re: problem with for and if MRAB <python@mrabarnett.plus.com> - 2015-01-05 14:48 +0000
  Re: problem with for and if Shiyao Ma <i@introo.me> - 2015-01-05 22:57 +0800
  Re: problem with for and if Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-01-06 12:12 +1100

csiph-web