python问题与密码检查时循环

发布于 2025-02-05 18:08:08 字数 1183 浏览 1 评论 0 原文

我最近才开始编程,但我仍然不清楚很多事情。但是,在此代码中,我只是不了解逻辑。我的前提是我不想使用导入或def(),但是我想了解如何推理该代码工作。

用户进入PWD,我希望有一张必须出现的数字和字母的支票。 首先是避免空PWD;在这一点上,我会肢解PWD并检查字母和数字。 如果我只输入数字或仅有工作的字母,则输入第二个期间和此处,但是如果我放置数字和字母,它将一直保持在循环中。 我尝试休息一下(已评论),但行不通。 如果有人能让我明白我在做错了什么,我会感激不尽。 在

pwd = input("Enter password.\n")

# This loop is used to check if the User has not typed in the password.

while len(pwd) == 0: # Enter the loop when it finds the pwd box empty
    print("You left the password box empty.\n")
    pwd = input("Enter the password.\n") # Query the password

n = ''.join(filter(str.isdigit, pwd))
char = pwd.isalpha()
num = n.isdigit()
# print(char, num)

# Control loop so that there are letters and numbers 

while (char== False and num == True) or (char== True and num == False):
    n = ''.join(filter(str.isdigit, pwd))
    char= pwd.isalpha()
    num = n.isdigit()
    print("Attention the password must contain numbers and letters.")
    pwd = input("Enter password again.\n") # Prompt for password
    # break
    if (char== True and num == True):
        break
print("Password contains numbers and letters. Accepted!")

PS:而不是我尝试使用IF时,而是在用户不断放置数字或仅放置字母,我无需尽可能地返回。

I am just getting into programming recently and still many things are not clear to me. However in this code I just don't understand the logic. I premise that I don't want to use either import or def(), but I want to understand how I have to reason to make this piece of code work.

A User enters the pwd, and I want there to be a check for numbers and letters that must be present.
The first while is to avoid empty pwd; at this point I dismember the pwd and check for letters and numbers.
Enter the second while and here if I enter only numbers or only letters it works but if I put numbers and letters it keeps staying in the loop.
I tried putting a break (is commented) but it doesn't work.
If anyone can make me understand what I am doing wrong I will be grateful.
Regards

pwd = input("Enter password.\n")

# This loop is used to check if the User has not typed in the password.

while len(pwd) == 0: # Enter the loop when it finds the pwd box empty
    print("You left the password box empty.\n")
    pwd = input("Enter the password.\n") # Query the password

n = ''.join(filter(str.isdigit, pwd))
char = pwd.isalpha()
num = n.isdigit()
# print(char, num)

# Control loop so that there are letters and numbers 

while (char== False and num == True) or (char== True and num == False):
    n = ''.join(filter(str.isdigit, pwd))
    char= pwd.isalpha()
    num = n.isdigit()
    print("Attention the password must contain numbers and letters.")
    pwd = input("Enter password again.\n") # Prompt for password
    # break
    if (char== True and num == True):
        break
print("Password contains numbers and letters. Accepted!")

PS: Instead of While I tried using if, but if the User keeps putting either only numbers or only letters I have no way to go back as I can with While.

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

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

发布评论

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

评论(1

孤独岁月 2025-02-12 18:08:08

首先,您遇到的问题是因为

char = pwd.isalpha()

此时 char 将始终是 false ,除非 pwd 只是alpha。因此,当您输入Alpha和编号示例的密码 123ASD 时, char 将始终是 false ,因为您直接使用 > isalpha() pwd 上的方法。您想在这里做的是使用用于检查数字的相同方法。这意味着在下面添加下面的行,

c = ''.join(filter(str.isalpha, pwd))

在此方式之前,

char = c.isalpha()

您要过滤所有alpha。

第二,在循环时在第二个内。 移动线路

print("Attention the password must contain numbers and letters.")
pwd = input("Enter password again.\n") # Prompt for password

在执行所有这些alpha和Digit滤波器之前,您需要在顶部 。因此,您将始终将存储在 pwd 中的最新值。

[可选]
此外,,使用您的方法,我建议对您的代码进行以下内容的重构。

pwd = input("Enter password.\n")
num = ''.join(filter(str.isdigit, pwd))
char = ''.join(filter(str.isalpha, pwd))
 
while not pwd or not num or not char:
    if not pwd:
        print("You left the password box empty.")
    else:
        print("Attention the password must contain numbers and letters.")
    pwd = input("Enter password again.\n")

    num = ''.join(filter(str.isdigit, pwd))
    char = ''.join(filter(str.isalpha, pwd))

print("Password contains numbers and letters. Accepted!")

First, Issue you are getting is because of the line

char = pwd.isalpha()

At this point char will always be False, unless pwd is only alpha. So, when you enter a password that is mix of alpha and number example 123asd, char will always be False as you are directly using isalpha() method on pwd. What you want to do here is use same approach you used to check numbers. That mean adding the line below,

c = ''.join(filter(str.isalpha, pwd))

before

char = c.isalpha()

This way you are filtering all the alpha out.

Second, inside your 2nd while loop. You need to move the lines

print("Attention the password must contain numbers and letters.")
pwd = input("Enter password again.\n") # Prompt for password

on top, before doing all those alpha and digit filters. So, that you will always have the latest value stored in the pwd.

[OPTIONAL]
Furthermore, using your same approach, I would suggest to refactor your code as following.

pwd = input("Enter password.\n")
num = ''.join(filter(str.isdigit, pwd))
char = ''.join(filter(str.isalpha, pwd))
 
while not pwd or not num or not char:
    if not pwd:
        print("You left the password box empty.")
    else:
        print("Attention the password must contain numbers and letters.")
    pwd = input("Enter password again.\n")

    num = ''.join(filter(str.isdigit, pwd))
    char = ''.join(filter(str.isalpha, pwd))

print("Password contains numbers and letters. Accepted!")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文