当前位置:   article > 正文

python if 语句的三种形式_python中if三个条件怎么写

python中if三个条件怎么写

if 语句的三种形式:


第一种:

if condition:
    语句1
    语句2
    ......
  • 1
  • 2
  • 3
  • 4

举例:

if True:
	print("T")
  • 1
  • 2

运行结果:
在这里插入图片描述


第二种:

if condition:
    语句1
    语句2
    ......
else:
    语句1
    语句2
    ......
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

举例:

price = 6300
money = 5000
if money >= price:
    print("购买英雄成功")
else:
    print("金币不足,请充值!!!")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

运行结果:
在这里插入图片描述


第三种:

if condition1:
    语句1
elif condition2:
    语句2
elif condition3:
    语句3
......
elif conditionN:
    语句N
else:(可选)
    语句N+1
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

举例:

score = 90
if score <= 60:
    print("D")
elif score <= 70:
    print("C")
elif score <= 85:
    print("B")
else:
    print("A")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

运行结果:
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/54737
推荐阅读
相关标签
  

闽ICP备14008679号