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


Groups > comp.lang.python > #71526

New to Python. For in loops curiosity

X-Received by 10.236.84.141 with SMTP id s13mr447750yhe.5.1400038712350; Tue, 13 May 2014 20:38:32 -0700 (PDT)
X-Received by 10.140.95.141 with SMTP id i13mr24912qge.3.1400038712287; Tue, 13 May 2014 20:38:32 -0700 (PDT)
Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!peer03.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!r10no4185766igi.0!news-out.google.com!gi6ni900igc.0!nntp.google.com!c1no6079974igq.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
Newsgroups comp.lang.python
Date Tue, 13 May 2014 20:38:32 -0700 (PDT)
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=71.191.145.179; posting-account=JgVcHwoAAABxVCFZ07cqn83kPmGFfR5x
NNTP-Posting-Host 71.191.145.179
User-Agent G2/1.0
MIME-Version 1.0
Message-ID <2f08e970-1334-4e7f-ba84-14869708a73b@googlegroups.com> (permalink)
Subject New to Python. For in loops curiosity
From Leonardo Petry <leonardo.petry.br@gmail.com>
Injection-Date Wed, 14 May 2014 03:38:32 +0000
Content-Type text/plain; charset=ISO-8859-1
X-Received-Bytes 1762
X-Received-Body-CRC 2720401921
Xref csiph.com comp.lang.python:71526

Show key headers only | View raw


Hi All,

So I am starting with python and I have been working on some simple exercises.

Here is something I found curious about python loops

This loop run each character in a string

def avoids(word,letters):
	flag = True
	for letter in letters:
		if(letter in word):
			flag = False
	return flag

The loop below (at the bottom) runs each line of the file

fin = open('wordplay.txt');
user_input = raw_input('Enter some characters: ')
count = 0
for line in fin:
    word = line.strip()
    if(avoids(word, user_input)):
    	count += 1;

This is just too convenient. 
Basically my question is: Why is python not treating the contents of wordplay.txt as one long string and looping each character?

Any comment is greatly appreciate. Thanks

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


Thread

New to Python. For in loops curiosity Leonardo Petry <leonardo.petry.br@gmail.com> - 2014-05-13 20:38 -0700
  Re: New to Python. For in loops curiosity John Gordon <gordon@panix.com> - 2014-05-14 03:46 +0000
  Re: New to Python. For in loops curiosity Rustom Mody <rustompmody@gmail.com> - 2014-05-13 20:48 -0700
  Re: New to Python. For in loops curiosity Ian Kelly <ian.g.kelly@gmail.com> - 2014-05-13 21:49 -0600
  Re: New to Python. For in loops curiosity Ben Finney <ben@benfinney.id.au> - 2014-05-14 14:03 +1000
  Re: New to Python. For in loops curiosity Roy Smith <roy@panix.com> - 2014-05-14 07:38 -0400
  Re: New to Python. For in loops curiosity Ned Batchelder <ned@nedbatchelder.com> - 2014-05-14 09:41 -0400

csiph-web