if 语句:
if 条件1: 执行语句 elif 条件2: 执行语句 else 条件3: 执行语句
Python 中用 elif 代替了 else if,所以if语句的关键字为:if – elif – else
注意:
1、每个条件后面要使用冒号 :,表示接下来是满足条件后要执行的语句块。
2、使用缩进来划分语句块,相同缩进数的语句在一起组成一个语句块。
3、在Python中没有switch – case语句。
Python中while语句的一般形式:
while 判断条件: 语句
同样需要注意冒号和缩进。另外,在Python中没有do..while循环。
实例:登陆验证,超过3次失败后推出。
count=0 while count != 3: name=input("name:") password=input("password:") if name=="xgh" and password=="123": print("输入正确") else : count=count+1 print("输入错误,请重新输入,你还有"+str(3-int(count))+"次机会")