while 条件满足:
语句1…
else:
循环完成后 要执行的语句
i = 0
while i < 3:
name = input(‘用户名:’)
passwd = input(‘密码:’)
if name == ‘root’ and passwd == ‘westos’:
print(‘登陆成功’)
# 跳出整个循环,不会再执行后面的内容
break
else:
print(‘登陆失败’)
print(‘您还剩余%d次机会’ %(2-i))
i += 1
else:
print(‘登陆次数超过三次,请等待100s后再次尝试登陆’)
while嵌套
输出9x9乘法表
i = 1
while i <= 9:
a = i
while a <= 9:
b = i * a
print(’%d*%d=%d\t’ % (i, a, b),end=’’)
a += 1
else:
print(’’)
i += 1
row = 1
while row <= 9:
col = 1
while col <= row:
print(’%d*%d=%d\t’ %(row,col,row*col),end=’’)
col += 1
# 手动换行
print(’’)
row += 1
\t:制表符,协助我们在输出文本的时候在垂直方向保持对齐
print(‘1\t2\t3’)
print(‘10\t20\t30’)
\n:换行符
print(‘hello \n python’)