Python:使用i控制

2024-09-29 21:39:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这个密码:

def the_flying_circus():
    if True and True and True and not False:
        print "Kevin stinkt"
    elif 10 < 4:
        print "Justin stinkt"
    else:
        print "herb-wuerzig"

当我print the_flying_circus打印Kevin stinkt时,None作为回报。我需要False作为在线教程的回报。为什么我要得到None,我怎样才能得到True?你知道吗


Tags: andthenonefalsetrue密码ifdef
3条回答

None是函数的返回值。没有显式return语句的函数将返回None。你知道吗

针对您的其他问题:

如果希望函数返回true,请将

return True

最后。如果你想让它返回false,把

return False

最后。你知道吗

如果函数不返回任何其他内容,则返回None,因此首先在函数内部打印,然后打印返回的None。你知道吗

如果您用return交换print语句,或者只调用the_flying_circus()而不是print the_flying_circus(),您将得到预期的结果。你知道吗

def the_flying_circus():
    if True and True and True and not False:
        return "Kevin stinkt"
    elif 10 < 4:
        return "Justin stinkt"
    else:
        return "herb-wuerzig"

然后可以运行函数并打印返回值:

print the_flying_circus()

或者你可以:

def the_flying_circus():
    if True and True and True and not False:
        print "Kevin stinkt"
    elif 10 < 4:
        print "Justin stinkt"
    else:
        print "herb-wuerzig"

只需调用函数而不打印返回值:

the_flying_circus()

所需代码如下:

# Make sure that the_flying_circus() returns True def the_flying_circus(antwort): if antwort > 5:
print "H" elif antwort < 5: print "A" else: print "I" return True

无论我输入什么,the_flying_circus总是返回True

相关问题 更多 >

    热门问题