私信  •  关注

Deepak Dixit

Deepak Dixit 最近创建的主题
Deepak Dixit 最近回复了
6 年前
回复了 Deepak Dixit 创建的主题 » 在python代码中使用pass语句的目的是什么?[复制品]

你可以这么说 通过 方法 NOP (无操作)操作。在这个例子之后,你会得到一个清晰的画面:

C程序

#include<stdio.h>

void main()
{
    int age = 12;

    if( age < 18 )
    {
         printf("You are not adult, so you can't do that task ");
    }
    else if( age >= 18 && age < 60)
    {
        // I will add more code later inside it 
    }
    else
    {
         printf("You are too old to do anything , sorry ");
    }
}

现在您将如何用python编写它:

age = 12

if age < 18:

    print "You are not adult, so you can't do that task"

elif age >= 18 and age < 60:

else:

    print "You are too old to do anything , sorry "

但是您的代码将给出错误,因为它需要在 否则如果 . 以下是 通过 关键字。

age = 12

if age < 18:

    print "You are not adult, so you can't do that task"

elif age >= 18 and age < 60:

    pass

else:

    print "You are too old to do anything , sorry "

现在我想你明白了。