如何使用Python限制文本中的某些单词

2024-07-03 06:35:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在制作一个程序,如果使用“通过”这个词,它会拒绝任何东西;可以是大写或小写字母

也就是说,如果正在使用pAss或pAss这个词

它也不能与addassfhsaPasS等其他单词一起使用

例如,我的输入是asdapaSS,然后被拒绝

条件是“不包含任何大小写字符组合中的字符串”pass“

下面的代码是我现在已经想到的

input1 = input("Please write a word: ")
lowercase = input1.lower()

if len(lowercase) == "pass":
    print("False")
else:
    print("True")

Tags: 字符串程序pass条件字符单词print大写
3条回答

这段代码使用正则表达式库检查刚刚输入的字符串的小写版本中是否没有“pass”。从正则表达式库中使用findall来解决这个问题

import re
string = input()

if "pass" not in  re.findall("pass",string.lower()):
    print("True")
    break
else:
    print("False")

您可以在operator中使用

if "pass" in input1.lower(): 
    print("false")
import re
while True:
  string = input()

  if "pass" not in  re.findall("pass",string.lower()):
    print("True")
    break
  else:
    print("False")

相关问题 更多 >