簡體   English   中英

Python-如果字母中包含“好”字和“壞”字,請打印出來

[英]Python - If a “good” word and a “bad” word contains in a letter, print it out

我有一個不好的單詞要列出,一個好單詞要列出。 我的想法是首先搜索好詞,然后查看字母中的哪個意思包含那些好詞,但是如果該意思還包含一個壞詞,那么它就不應打印出意思。

等等:

Good word: "Lo", "Yes"
Bad word: "Hate", "Not"
text_roman: "Hello guys. My name is Lo and I hate to code :')"

意思是:“大家好。我叫Lo,我討厭編碼:')” <-“ 只是個玩笑!

因此,如果它搜索該含義,則它應該告訴我們存在一個包含好意思的意思,然后檢查它是否包含壞詞。 如果是這樣,那么我們不想打印出含義,但是如果它不包含任何不良詞,那么我們應該將其打印出來。

我嘗試編碼的方式是:

text_roman = "Hello guys. My name is Lo and I hate to code :')"
good_word = ["Lo", "Yes"]
bad_word = ["Hate", "Not"]
for text in good_word:
   if text in text_roman:
      print("Yay found word " + text_roman)
      for bad_text in bad_word:
         if bad_text not in text_roman:
             print("Yay no bad words!")

當我嘗試這樣做時,不幸的是,輸出也給出了所有包含不良詞的詞

我將首先遍歷壞詞,然后跳過它們。 然后,如果沒有跳過,請檢查一個好單詞

good_word = ["Lo", "Yes"]
bad_word = ["Hate", "Not"]

has_bad = False
for b in bad_word:
   if b in text_roman:
       has_bad = True 
       continue
   for g in good_word:
      if g in text_roman:
          print("Yay found word " + g + " in text_roman")
if not has_bad:
    print("Yay no bad words!")

注: in區分大小寫,因此"Hate" in "I hate case-sensitivity"將假

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM