if语句
1,单 if 条件满足:
if 条件:
缩进 结果
name = "张浩"
input_name=input("Input your name :")
if input_name == name:
print("Same name to me !")
2,2条件满足其中一个条件:if else
if 条件:
缩进 结果
else:
缩进 结果
name = "张浩"
input_name=input("Input your name :")
if input_name == name:
print("Same name to me !")
else:
print("Different name to me !")
3,多条件满足一个或都不符合 if elif elif elif
-
if 条件:
缩进 结果
elif 条件:
缩进 结果
elif 条件:
缩进 结果
name1 = "张浩"
name2 = "Jack"
name3 = "Brows"
input_name=input("Input your name :")
if input_name == name1:
print("Same name to me !")
elif input_name == name2:
print("My bestfriend !")
elif input_name == name3:
print("My best friend's girlfriend !")
4,多条件符合一个 if elif elif else
-
if 条件:
缩进 结果
elif 条件:
缩进 结果
elif 条件:
缩进 结果
else :
缩进 结果
name1 = "张浩"
name2 = "Jack"
name3 = "Brows"
input_name=input("Input your name :")
if input_name == name1:
print("Same name to me !")
elif input_name == name2:
print("My bestfriend !")
elif input_name == name3:
print("My best friend's girlfriend !")
else:
print("I do not know you !")
5,if 嵌套
-
if 条件:
if 条件:
缩进 结果
your_score = int(input("Input your score :"))
if your_score > 90:
print("You are great !")
if your_score == 100:
print("You are No.1 !")
else:
print("Come on ,you can be No.1 next !")
else:
print("You need to study hard !")