python if...else语句

if else语句

Python流程控制

IF条件分支语句Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块。

- if单分支语句

    if 逻辑语句:
        语句(块)

demo:

    print(111)
    if True:
        print(222)
    print(333)

运行:

    111
    222
    333

如果改成如下:

    print(111)
    if False:
        print(222)
    print(333)

运行:

    111
    333True或者False换成逻辑语句。

练习:写一个登录判断:
输入用户名和密码;输入正确,恭喜登录成功,否则,请重试。

  • if多分支语句

    Python中用elif代替了else if,所以if语句的关键字为:if-elif-else。

    • 1.每个条件后面要使用冒号:表示接下来是满足条件后要执行的语句块。
    • 2.使用缩进来划分语句块,相同缩进数的语句在一起组成一个语句块。
    • 3.zai Python中没有switch-case语句。
if 逻辑语句1:
   语句()1
elif 逻辑语句2:
   语句()2
elif 逻辑语句3:
   语句()3
......
elif  逻辑语句n-1:
    语句()n-1
else:
    语句()n

要求用户输入0-100的数字后,你能正确打印他的对应成绩

score = int(input("输入分数"))
if score>100:
	print("我擦,最高分才100分,附加题也有分,666")
elif score>=90:
   print("A")
elif score>=80:
   print("B")
elif score>=60:
   print("C")
elif score>=40:
    print("D")
else:
    print("太笨了...E")

猜你喜欢

转载自blog.csdn.net/qq_38499337/article/details/89479549