python基础语法_8_for循环嵌套

#for 循环的嵌套结构:
# for 变量1 in 可迭代对象:
#     for 变量2 in 可迭代对象:
#         语句1
#         语句2
#     语句3
#     语句4

案例1:
打印
*****
****
***
**
*
for i in range(0,5):
    for j in range(0,5-i):
        print('*',end='')
    print()

案例2:打印乘法口诀表
for i in range(1,10):                               #行数
    for j in range(1,i+1):                          #列数
        print('%i*%i=%i' %(j, i, i*j), end='\t')
    print()

案例3:1234四个数,求无重复且互不相同的组合
for i in (1,2,3,4):
    for j in (1,2,3,4):
        for k in (1,2,3,4):
            if (i!=j) and (i!=k) and (j!=k):
                print(i,j,k)

案例4:登录
error_num=0
yonghu= True
while yonghu:
    print('===登录系统===')
    user = input('请输入用户名:')
    password = input('请输入密码:')
    if user =='guanxiu' and password =='123456':
        print('登录成功')
        yonghu =False
    elif user=='' or password=='':
        print('用户名或密码不能为空!')
    elif user=='' and password=='':
        print('请输入用户名!')
    else:
        error_num+=1
        if (error_num==3):
            print('用户名或密码错误超过3次,账号冻结!')
            yonghu=False
        else:
            print('用户名密码不匹配,请重新输入!')

你可能感兴趣的:(python基础语法_8_for循环嵌套)