python - 尝试检查满足这些条件的字符串

标签 python if-statement

尝试使用以下代码检查字符串:

def check_password(x):
  if has_unique_letters(x) == False:
    print "Warning! Please ensure letters are not repeated."
  if has_even_vowels(x) == False:
    print "Warning! Please ensure password contains an even number of vowels."
  if has_special_character(x) == False:
    print "Warning! Please ensure password contains at least one of {@, #, *, $}"
  if has_divisible_numbers(x) == False:
    print "Warning! Please ensure all numbers are divisible by 2 or 3."
  print "Sorry, your password does not meet our criteria."
print "Congratulations, your password meets our criteria."

x = raw_input("Please set your password:")
check_password(x)


但是我对如何制作感到困惑:

print "Sorry, your password does not meet our criteria."

和:

print "Congratulations, your password meets our criteria." 

这两个句子正确显示。

我打算在不满足其中一个条件时显示每个 “警告!...” 以及 “抱歉,...”,同时显示 "Congratulation,..." 当所有条件都满足时。
但是对于我所拥有的,"Sorry,..." 行将始终显示,而 "Congratulations,..." 行不会显示。

我知道我一定做错了,但我该如何解决?
谢谢!

最佳答案

创建一个包含密码中所有错误的列表。如果有错误,声明密码不符合标准,然后打印所有密码错误类型。否则,打印它符合条件。

def check_password(password):
    errors = []
    if not has_unique_letters(password):
        errors.append("Warning! Please ensure letters are not repeated.")
    if not has_even_vowels(password):
        errors.append("Warning! Please ensure password contains an even number of vowels.")
    if not has_special_character(password):
        errors.append("Warning! Please ensure password contains at least one of {@, #, *, $}")
    if not has_divisible_numbers(password):
        errors.append("Warning! Please ensure all numbers are divisible by 2 or 3.")
    if errors:
        print "Sorry, your password does not meet our criteria."
        for e in errors:
            print(e)
    else:
        print "Congratulations, your password meets our criteria."

password = raw_input("Please set your password:")
check_password(password)

关于python - 尝试检查满足这些条件的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46519558/

相关文章:

python - 在 Pandas 中执行连接

r - 如何根据特定条件交换 R 中的两列?

python - 在 Python 的 If 语句中使用 re.match

java - 嗨,我试图让我的 if 语句等于多个数字

c# - 如果 else 语句不起作用

css - 如何使用 CSS if..else 来改变主体背景颜色

python - 调用 celery.task.chunks 的正确方法是什么?

python - 为什么seaborn的pairplot没有绘制第一个情节?

python - Pandas:read.csv() - 只读具有特定列长度的行

python - 如何将所有 twill 命令放在 .py 文件中的一段代码中?