(1)while语句的基本格式
while 条件:
条件满足时,做的事情1
条件满足时,做的事情2
...做的事情可以有很多
示例
#1.定义计数器
i = 0
#2.开始循环
while i < 3:
#循环内需要做的事情
print('hello python')
#处理计数器
i += 1
(2)使用while语句实现死循环
while True:条件永远成立
print('hello python')
(3)求1+2+3+…+100的结果
(4)用while改写之前的用户登录程序
trycount = 0
while trycount < 3:
name = input('用户名: ')
passwd = input('密码: ')
if name == 'root' and passwd == 'westos':
print('登录成功!')
break
else:
print('登录失败')
print('您还剩余%d次机会' %(2-trycount))
trycount += 1
else:
print('失败超过3次,请稍后再试!')