On Wed, Dec 24, 2008, bob gailer wrote: > Emad Nawfal (???? ????) wrote: >> Hi Tutors, >> I want a function that acts like the startswith method, but can take >> multiple prefixes. As an amateur programmer, I came up with this one, >> and it works fine, but my experience tells me that my solutions are >> not always the best ones around. Can you please tell me what a better >> option might be: ... > Above does a lot more work than necessary. Try: > > def beginsWith(word, listname): > for prefix in listname: > if word.startswith(prefix): > return True
It might be more useful to return the prefix that matched as the caller already knows what ``word'' is. Bill -- INTERNET: b...@celestial.com Bill Campbell; Celestial Software LLC URL: http://www.celestial.com/ PO Box 820; 6641 E. Mercer Way Voice: (206) 236-1676 Mercer Island, WA 98040-0820 Fax: (206) 232-9186 Basic Definitions of Science: If it's green or wiggles, it's biology. If it stinks, it's chemistry. If it doesn't work, it's physics. _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor