我最近才开始编程,但我仍然不清楚很多事情。但是,在此代码中,我只是不了解逻辑。我的前提是我不想使用导入或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.
发布评论
评论(1)
首先,您遇到的问题是因为
此时
char
将始终是false
,除非pwd
只是alpha。因此,当您输入Alpha和编号示例的密码123ASD
时,char
将始终是false
,因为您直接使用> isalpha()
pwd
上的方法。您想在这里做的是使用用于检查数字的相同方法。这意味着在下面添加下面的行,在此方式之前,
您要过滤所有alpha。
第二,在循环时在第二个
内。 移动线路
在执行所有这些alpha和Digit滤波器之前,您需要在顶部 。因此,您将始终将存储在
pwd
中的最新值。[可选]
此外,,使用您的方法,我建议对您的代码进行以下内容的重构。
First, Issue you are getting is because of the line
At this point
char
will always beFalse
, unlesspwd
is only alpha. So, when you enter a password that is mix of alpha and number example123asd
,char
will always beFalse
as you are directly usingisalpha()
method onpwd
. What you want to do here is use same approach you used to check numbers. That mean adding the line below,before
This way you are filtering all the alpha out.
Second, inside your 2nd
while loop
. You need to move the lineson 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.