Python 密码中的一个特殊字符

发布于 2025-01-18 13:18:45 字数 499 浏览 1 评论 0原文

我有一个小问题,因为我想检查用户密码并强制必须使用一个特殊字符。但在我的输出中,即使创建了密码,我也会收到无效消息。我该如何修复这个东西?

special_character= '$#!'


created = False
while not created:
    password = input('Type your password: ')
    for char in special_character:
        if char in password:
            print('Password Created!!')
            created = True
    else:
        print('Sorry, try again!')

Output:
Type your password: MyStronPassword123$
Password Created!!
Sorry, try again!

我怎样才能丢弃这个文本:“抱歉,再试一次”

I have a tiny problem, because i want to check user password and force so that must using one special character. But in my output i get a invalid message even though password is created. How can i repair this thing?

special_character= '$#!'


created = False
while not created:
    password = input('Type your password: ')
    for char in special_character:
        if char in password:
            print('Password Created!!')
            created = True
    else:
        print('Sorry, try again!')

Output:
Type your password: MyStronPassword123$
Password Created!!
Sorry, try again!

How can i discard this text: "Sorry try: again"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绝不服输 2025-01-25 13:18:45

您应该使用if语句查看是否创建了密码。

special_character= '$#!'

created = False
while not created:
    password = input('Type your password: ')
    for char in special_character:
        if char in password:
            print('Password Created!!')
            created = True
    if not created:
        print('Sorry, try again!')

输出:

输入密码:Mystronpassword123 $

密码创建!

You should use an if statement to see if the password is created.

special_character= '$#!'

created = False
while not created:
    password = input('Type your password: ')
    for char in special_character:
        if char in password:
            print('Password Created!!')
            created = True
    if not created:
        print('Sorry, try again!')

Output:

Type your password: MyStronPassword123$

Password Created!!

深海少女心 2025-01-25 13:18:45

尝试使用以下方法:

sc = '$#!'
ps = "test!"
for i in sc:
    if i in ps:
        print("GOOD")
        break

Try using the following method:

sc = '$#!'
ps = "test!"
for i in sc:
    if i in ps:
        print("GOOD")
        break
ζ澈沫 2025-01-25 13:18:45

您的 else 语句将始终被执行,我想您希望仅当 for 循环被中断时才执行它,在这种情况下添加 break

special_character= '$#!'


created = False
while not created:
    password = input('Type your password: ')
    for char in special_character:
        if char in password:
            print('Password Created!!')
            created = True
            break
    
    else:
        print('Sorry, try again!')

Your else statement will always be executed, I suppose you want it to be executed only if the for loop is interrupted in that case add break :

special_character= '$#!'


created = False
while not created:
    password = input('Type your password: ')
    for char in special_character:
        if char in password:
            print('Password Created!!')
            created = True
            break
    
    else:
        print('Sorry, try again!')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文