python - 如何检查某些特定字符不在字符串中
问题描述
我正在尝试检查一个符号(例如(a
或b
))是否不在字符串中。我写了下面的代码,但它不能正常工作。
name = input("please enter name ")
while ("a" or "b" or "c" or "d") in name:
name = input("please retry")
解决方案
一种可能的变体是使用集合:
while {'a', 'b', 'c', 'd'}.intersection(name):
您可以将此集合存储到某个变量:
bad_characters = {'a', 'b', 'c', 'd'}
name = input('name:')
while bad_characters.intersection(name):
name = input('name:')
推荐阅读
- r - 将闪亮的调查结果下载到 CSV
- android - 如何修复 Android Studio 项目中未找到的 SDK
- mongodb - 更新嵌套数组中的特定元素
- javascript - 如何在 JAVASCRIPT 中识别具有 ClassName 和 onlick 事件的按钮?
- node.js - 来自一个非常简单的查询程序的“.then 不是函数”
- html - Layout styling with responsive design using CSS
- python - 使用烧瓶将表单数据和列表值传递到 HTML 页面
- python - Where do I add in an re.search in this python code?
- python - Python:将CSV文件的多列转换为嵌套的Json
- python - Is there a way to insert a list into the .format method in python?