Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #83245
| References | <a71e126d-4b27-42a3-8103-3381b62794d2@googlegroups.com> |
|---|---|
| Date | 2015-01-05 15:41 +0100 |
| Subject | Re: problem with for and if |
| From | Chris Warrick <kwpolska@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.17397.1420468894.18130.python-list@python.org> (permalink) |
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
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