-1

While learning python, I was messing around with the replace() method and tried making this program:

message = raw_input("Message to find the part of speech: ")
coordconj = "for" or "and" or "nor" or "but" or "or" or "yet" or "so"

print message.replace(coordconj, "coordinating conjunction")

If I run this with the input "name1 for name2". The output is "name1 coordinating conjunction name2" But with "name1 and name2" as the input it prints "name1 and name2"

I also tried:

message = raw_input("Message to find the part of speech: ")


print message.replace("for" or "and" or "nor" or "but" or "or" or "yet" or "so", "coordinating conjunction")

But that didn't work either. It only replaces "for" with "coordinating conjunction". Is there any way to have all words in the variable coorcon be replaced with "coordinating conjunction" without using a bunch of if statements? Thanks in advance.

th4t gi
  • 139
  • 2
  • 5
  • Briefly: `re.sub` is probably the easiest way to do that, and I suggest you try printing the value of `coordconj`, because `or` doesn't do anything close to what you think it does. – TigerhawkT3 Mar 26 '17 at 21:28
  • Refresh the page. The links are placed between the title and body. – TigerhawkT3 Mar 26 '17 at 21:33
  • I can help you a little by letting you know that your expression `"for" or "and" or "nor" ...` is valid but not what you want. Rather, you are doing a logical operation and due to [short-cicruit logic](http://stackoverflow.com/questions/43032801/whats-the-value-of-short-circuit-of-python/43032929#43032929), the first string is being returned. That is, your logical expression evaluates to just "for". – Mark Mar 26 '17 at 21:54

1 Answers1

-1
"for" or "and" or "nor" or "but" or "or" or "yet" or "so"

is just "for" since a non-empty string is Truthy in Python and or is evaluated lazily.

You can check in Python console :

>>> "for" or "and" or "nor" or "but" or "or" or "yet" or "so"
'for'

Here's a possible way to solve your problem :

import re

def find_speech_part(matchobj):
  word = matchobj.group(0)
  if word.lower() in ["for","and","nor","but","or","yet","so"]:
    return "coordinating conjunction"
  else:
    return word

print(re.sub('\w+', find_speech_part, 'For A but not for B because neither C nor D'))
# coordinating conjunction A coordinating conjunction not coordinating conjunction B because neither C coordinating conjunction D

It's been adapted from this previous answer.

Community
  • 1
  • 1
Eric Duminil
  • 52,989
  • 9
  • 71
  • 124
  • 1
    I see, I'll try something else. Thanks – th4t gi Mar 26 '17 at 21:40
  • 1
    There's really no need to answer common duplicates like this - you didn't even provide a solution. This would've been better suited as a comment. – TigerhawkT3 Mar 26 '17 at 21:41
  • 1
    There's also _really_ no need to copy existing answers. – TigerhawkT3 Mar 26 '17 at 21:44
  • 1
    All of the originals will help the OP. That's why I put them there. If OP doesn't understand some of the terms used in those answers, they should _research_ them, not wait for someone else who's just as likely as they to use this site as a coding service. – TigerhawkT3 Mar 26 '17 at 21:52
  • 1
    When I find the anonymous user who downvoted your post, I'll let them know. – TigerhawkT3 Mar 26 '17 at 22:15
  • @TigerhawkT3: If you downvote someone and think you have a valid reason to do so, just say so and be done with it. That's fine with me. Don't try to hide behind SO's anonymous voting. – Eric Duminil Mar 27 '17 at 08:14
  • Maybe it's the same person who's been downvoting my questions. I'll keep an eye out for them. – TigerhawkT3 Mar 27 '17 at 08:23
  • @TigerhawkT3: It's harder to detect sarcasm on the Internet. If you mean to imply I participate in revenge downvoting : I don't. It's childish and encourages the exact same behaviour I'm complaining about. – Eric Duminil Mar 27 '17 at 08:36