如果所有字符都是数字,则
我确信答案就在眼前,但当我输入正确的条件时,我似乎不知道如何修复第一个if语句的返回。
create_password = input("Enter password here: ")
if len(create_password) > 6 and create_password.isdigit() > 0:
print("Your account is ready!")
elif len(create_password) < 6 and create_password.isdigit() < 1:
print("Password must be more than 6 characters and must include a number")
elif len(create_password) > 6 and create_password.isdigit() == 0:
print("Password must include a number")
else:
print("Your password sucks")
假设我输入elephant100,我正试图得到提示为";您的帐户已准备就绪&";。但令我沮丧的是,上面印着";密码必须包括一个数字";我不知道为什么。我的其他条件与正确的输入相匹配,但这是唯一不起作用的条件。
.isdigit()
方法返回True
,否则返回False
。因此,在这种情况下,它返回False
,因为您的字符串包含e、l、p等字母。因此,语句print("Your account is ready!")
永远不会被执行。
原来我需要使用isalnum((、isnumeric((和isalpha((来解决我的问题。谢谢穆罕默德·贾西姆帮我弄明白!这是我修改后的代码。
if create_password.isnumeric() == True:
print("Password must be more than 6 characters and include letters")
elif create_password.isalpha() == True:
print("Password must be more than 6 characters and include numbers")
elif len(create_password) > 6 and create_password.isalnum() == True:
print("Your account is ready!")
else:
print("Your password sucks. Must be more than 6 characters and contain only letters and numbers")