Python 基础(三)之流程控制(1)——if…else

if 语句

 1 x = int(input("Please enter an integer"))
 2 if x<0:
 3     x = 0
 4     print("Negative changed to zero")
 5 elif x ==0:
 6     print("Zero")
 7 elif x == 1:
 8     print("Single")
 9 else :
10     print("More")

可能会有零到多个 elif 部分,else 是可选的。关键字 ‘elif’ 是 ’else if’ 的缩写,这个可以有效地避免过深的缩进。ifelifelif … 序列用于替代其它语言中的 switchcase 语句。

例程:

1 _username = "Brain"
2 _password = "12345"
3 username = input("username:")
4 password = input("password:")
5 
6 if username ==_username and password == _password:
7     print("Welcome user {name} login...".format(name=username))
8 else:
9     print("Invalid username or password!")
原文地址:https://www.cnblogs.com/yz9110/p/8166178.html