learning with python question (HtTLaPP)

umpsumps at gmail.com umpsumps at gmail.com
Sat Apr 26 16:00:00 EDT 2008


Hello all,

I've been trying to teach myself python from "How to Think Like a
Python Programmer" and have been trying to write a script that checks
'words.txt' for parameters (letters) given.  The problem that is the i
can only get results for the exact sequnce of parameter 'letters'.
I'll spare posting all the different ways I've tried to search for
specific letters.  But they are all generally:

for line in fin:
	for linechar in line:
		for ch in letters:

or the "for linechar in line:" and "for ch in letters:" get switched..
I'm getting really frustrated to say the least.

 What alternative method could I use that isn't too advanced?  Any
tips/suggestions on the code itself would be greatly appreciated, and
tips for learning in general.

here is the code that returns a certain sequence:

>>> def searchtxt(letters):
	fin = open('words.txt')
	words = ""
	index = 0
	count = 0
	for line in fin:
		index +=1
		if letters in line.strip():
			count += 1
			words = line.strip() + '\n' + words

	print words
	print index, 'lines searched..', count, letters, 'words present'

Thank you in advance.



More information about the Python-list mailing list