How to use the pass statement?

前端 未结 15 2496
旧时难觅i
旧时难觅i 2020-11-22 15:03

I am in the process of learning Python and I have reached the section about the pass statement. The guide I\'m using defines it as being a Null sta

15条回答
  •  渐次进展
    2020-11-22 15:29

    You can say that pass means NOP (No Operation) operation. You will get a clear picture after this example :-

    C Program

    #include
    
    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 ");
        }
    }
    

    Now how you will write that in 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 "
    

    But your code will give error because it required an indented block after elif . Here is the role of pass keyword.

    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 "
    

    Now I think its clear to you.

提交回复
热议问题