learning with python question (HtTLaPP)
umpsumps at gmail.com
umpsumps at gmail.com
Sat Apr 26 19:50:57 EDT 2008
ok.. I finally made something that works.. Please let me know what you
think:
>>> def lines(letters):
fin = open('words.txt')
count = 0
rescount = 0 # count the number of results
results = "" # there are words that contain the letters
for line in fin:
needs = 0
x = str(line.strip())
for ch in letters:
if ch not in x:
pass
else:
needs = needs + 1
if needs == len(letters):
rescount += 1
results = results + '\n' + x
count += 1
print count, 'lines searched'
print results, '\n'
print 'result count is: ', rescount
On Apr 26, 4:02 pm, "Eric Wertman" <ewert... at gmail.com> wrote:
> > Is the way I wrote the function inherently wrong? What I wrote
>
> I would not say that. I think a lot of people probably start off like
> that with python. You'll find in most cases that manually keeping
> counters isn't necessary. If you really want to learn python though,
> I would suggest using built in functions and libraries as much as
> possible, as that's where the real power comes from (IMO).
>
> > returns the sequence, however I'm trying to make the output match for
> > the letters in the string entered, not necessarily the string
> > sequence.
> > Only the sequence shows up 'uzi'. I don't get words like 'unzip' or
> > 'Zurich' . I've only barely started on invocation and maybe writing
> > something like I'm describing is above what level I'm currently at.
>
> This would be a more difficult approach.. Where you are doing the
> comparison step:
>
> if letters in line.strip():
>
> It's trying to match the exact string "uzi", not any of the individual
> letters. You would need to look for each letter independently and
> then make sure they were in the right order to match the other words.
More information about the Python-list
mailing list