如何返回一个布尔值并在python中打印一条消息

时间:2017-05-31 05:09:38

标签: python

例如,我想返回一个布尔值False并在python中同时打印一条消息("它没有通过测试"),怎么做?

1 个答案:

答案 0 :(得分:1)

你在找这个吗?

def this_returns_false(<arguments>):
    """
    Do stuff here
    """
    return False

if not this_returns_false(<args>): # not accesses the function for False statement now
    print "It did not pass the test"

可能的简写:

print "It did not pass the test" if not this_returns_false else ""

OP的代码:

def password_check(password): 

    upper_letter = str.upper(lower_letter) 

    if ((count_digit(password) == 0) or (count_Lletter(password) == 0) or (count_Uletter(password) == 0)): 
        return False and print("it did not pass the test")

在OP代码后编辑:

def password_check(password): 

    upper_letter = str.upper(lower_letter) 

    if (not count_digit(password)) or (not count_Lletter(password)) or (not count_Uletter(password)):  
        # not is the same as !

        print("it did not pass the test")
        return False

{b}返回语句之前执行print语句。