gpt4 book ai didi

Python如何检查输入是否只包含某些字符?

转载 作者:行者123 更新时间:2023-12-01 02:28:04 27 4
gpt4 key购买 nike

我正在尝试让我的代码检查用户输入是否仅包含以下字符:"!$%^&*()_-+="

如果它包含任何其他字符,则应减去分数。我尝试了这个,但它不能正常工作:

if "a-z" not in password and "A-Z" not in password and "0-9" not in password:
points = points - 1

我该如何解决这个问题?

谢谢

最佳答案

您可以通过转义上面列出的字符来使用正则表达式:

import re
s = "_%&&^$"
if not re.findall("^[\!\$\%\^\&\*\(\)\_\-\+\=]+$", s):
points -= 1

关于Python如何检查输入是否只包含某些字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47164260/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com