zoukankan      html  css  js  c++  java
  • python的if判断补充

    python的if判断补充

    exit_flag = False  # 标识符
    
    if exit_flag == False:
        print('exit_flag == False')
        exit_flag = True
        
    else:
        print('exit_flag == True')
    

    你们能猜到这个程序的运行结果对不对?

    exit_flag == False
    

    那么,我想让第10行的print('exit_flag == True')也执行怎么办呢?

    exit_flag = False  # 标识符
    
    if exit_flag == False:
        print('exit_flag == False')
        exit_flag = True
    
    if exit_flag == True:
        print('exit_flag == True')
    

    运行结果:

    exit_flag == False
    exit_flag == True
    

    为什么第一个代码和第二个代码输出不一样?

    首先我们看一下第一个代码,程序从上到下开始运行,标识符等于False,此时进行if判断,然后输出print('exit_flag == False'),然后再让标识符等于True,然后程序会怎么样?会结束,为什么?因为if...else...就是单分支语句,要么执行if里的东西,要么执行else里的东西。

    看第二个代码,程序从上开始往下执行,标识符等于False,此时进行if判断,然后输入print('exit_flag == False'),然后再让标识符等于True,然后,然后,程序再往下执行,因为此时是两个if,相当于是多分支的语句,所以还会打印出来exit_flag == True。

  • 相关阅读:
    topcoder srm 681 div1
    topcoder srm 683 div1
    topcoder srm 684 div1
    topcoder srm 715 div1
    topcoder srm 685 div1
    topcoder srm 687 div1
    topcoder srm 688 div1
    topcoder srm 689 div1
    topcoder srm 686 div1
    topcoder srm 690 div1 -3
  • 原文地址:https://www.cnblogs.com/xiaoyafei/p/9004182.html
Copyright © 2011-2022 走看看