如何检查输入内容是否在Python中包含特殊字符?

问题描述 投票:0回答:2

我是一名高中程序员,目前正在用Python编写代码以增强我的编程技能,并正在创建一个基本的注册页面。我希望我的代码在用户输入的用户名或密码无效的情况下提醒用户,但是到目前为止,我看到的所有答案都不太有用。我发现“ ^ [a-zA-Z0-9] * $”到目前为止对于检测特殊字符(例如星号和句号)最有用,但是它似乎仅在用户使用上述所有字符时才起作用满足要求的字符范围内的字符。我将在下面显示我的代码:

 while not chosen_user:
     chosen_user = input("""Enter the username that you would like. Please keep it between 6 and 15 
     characters. Do not use special characters such as '&', '@', or '!' in your username. Underscores are allowed.\n>""")
    if (len(chosen_user) >= 6 or len(chosen_user) <= 15) and "^[a-zA-Z0-9_]*$" in chosen_user:
        chosen_user = True
        usernames_passwords[chosen_user] = ''
    else:
        print('Username does not meet the requirements. Please try again.')
        chosen_user = False
 while not chosen_pass:
     chosen_pass = input("""Enter a password that contains no special characters. Password must be at 
        least 6 characters.\n>""")
        if len(chosen_pass) >= 6 and "^[a-zA-Z0-9]*$" in chosen_pass:
            chosen_pass = True
            usernames_passwords[chosen_user] = chosen_pass
        else:
            print('Password is invalid. Please try again.')
            chosen_pass = False

我不知道如何仅使用some个字符来获得密码批准,并且我已经苦苦挣扎了一段时间。你能帮忙的话,我会很高兴。

**另外,请记住,我正在读高中,因此希望尽量简化该代码。导入是可以的,但是我想了解如何以及何时使用这些东西以备将来使用。谢谢!!

我是一名高中程序员,目前正在用Python编写代码以增强我的编程技能,并正在创建一个基本的注册页面。我希望我的代码在用户输入用户名时警告用户...

python string
2个回答
0
投票

尝试一下:


0
投票

您可以使用正则表达式简化此操作:

© www.soinside.com 2019 - 2024. All rights reserved.