[Tutor] variabel from raw input into re.search?

Alan Gauld alan.gauld at btinternet.com
Fri Jan 7 17:50:32 CET 2011


"Tommy Kaas" <tommy.kaas at kaasogmulvad.dk> wrote

>I try to write a program, where the user can write a word or a name 
>and I
> will tell how many times the subject was mentioned in a text, I'm 
> hosting.
>
> I guess it's possible but not this way it seems?
>
> The re is only searching for the word "name" and not the variable 
> name

You don't need a regex here.

name = raw_input('Who would you like to check'? ')
with  open("text.txt", "r") as mytxt:
    antal = [line for line in mytxt if name in line]

print name,' was mentioned ', len(antal),' times in the text'

Should suffice.

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/




More information about the Tutor mailing list