Question

So im new at python and i would like some help in making code where: When an input is typed if the input has a minimum of three words that match any one thing on a list it would replace the input with the text in the list that matches the criteria

Example: Jan -Scans List- -Finds Janice- -Replaces and gives output as Janice Instead of Jan- Janice

So far

getname = []
for word in args
 room.usernames.get(args,word)

Room.usernames is the list and args input

Error List item has no attribute .get

there is a module used and its ch.py located at http://pastebin.com/5BLZ0UA0

Was it helpful?

Solution

You will need to:

  • make a replacement words dictionary
  • get some input
  • do sanity checking to make sure it fits your parameters
  • split it into a list
  • loop through your new list and replace each word with its replacement in your dict, if it is in there

I won't write all of this for you. But here's a tip for how to do that last part: use the get method of dicts - it allows you to provide a "fallback" in case the word is not found in the dict. So just fall back to the word itself.

replacement_words = {'jan':'janice','foo':'bar'}
my_list = ['jan','is','cool']

[replacement_words.get(word,word) for word in my_list]
Out[41]: ['janice', 'is', 'cool']

OTHER TIPS

You Could try this

getname = []
for word in args
  "%s"% (room.usernames).get(args,word)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top