While and For循环

While and For循环

While循环

  1.while单独使用

    while 条件:
      条件成立之后循环执行的子代码块

    每次执行完循环体子代码之后都会重新判断条件是否成立
    如果成立则继续执行子代码如果不成立则退出

while True:
    # 1.获取用户输入的用户名和密码
    username = input('username>>>:')
    password = input('password>>>:')
    # 2.判断用户名和密码是否正确
    if username == 'jason' and password == '123':
        print('来宾三位')
    else:
        print('去你妹的 没钱滚蛋')

  

  2.while+break

    while + break
    break结束本层循环

#while True:
    # 1.获取用户输入的用户名和密码
    username = input('username>>>:')
    password = input('password>>>:')
    # 2.判断用户名和密码是否正确
    if username == 'jason' and password == '123':
       print('来宾三位')
        # 直接结束本层循环
        break
    else:
        print('去你妹的 没钱滚蛋')

   

  3.while+continue

    while+continue

    continue跳过本次循环

# 1.使用while循环打印出0-10
count = 0while count < 11:    
    print(count)    
    count += 1
# 2.使用while循环打印出0-10但是不打印4
# 1.定义一个起始变量
count = 0
# 2.循环
while count < 11:
    # 5.判断 如果count为4则不打印
    if count == 4:
        count += 1
        # 跳过本次循环 开始下一次循环
        continue
    # 3.打印变量的值
    print(count)
    # 4.变量值自增1
    count += 1

 

    continue会让循环体代码直接回到条件判断处重新判断

 

  4.while+else

    while+else

    else当前程序整个自然结束时执行子代码块

    如果人为结束则不执行(while 执行break结束的就为人为结束)

count = 0
while count < 5:
     print(count)
     count += 1
else:
     print('嘿嘿嘿')  # 会执行else子代码

count = 0
while count < 5:
    if count == 3:
        break
    print(count)
    count += 1
else:
    print('嘿嘿嘿')  # 不会执行else子代码

  

  

  5.while循环的嵌套使用

# while嵌套
while True:
    # 1.获取用户输入的用户名和密码
    username = input('username>>>:')
    password = input('password>>>:')
    # 2.判断用户名和密码是否正确
    if username == 'jason' and password == '123':
        print('来宾三位')
        while True:
            cmd = input('请输入您的指令>>>:')
            # 判断用户是否想退出
            if cmd == 'q':
                break
            print('正在执行您的指令:%s' % cmd)
        break
    else:
        print('去你妹的 没钱滚蛋')

 

  6.全局标志位

    本题中:flag为标志位   一经改变 全局都会改变

flag = True
while flag:
    # 1.获取用户输入的用户名和密码
    username = input('username>>>:')
    password = input('password>>>:')
    # 2.判断用户名和密码是否正确
    if username == 'jason' and password == '123':
        print('来宾三位')
        while flag:
            cmd = input('请输入您的指令>>>:')
            # 判断用户是否想退出
            if cmd == 'q':
                flag = False
            print('正在执行您的指令:%s' % cmd)
    else:
        print('去你妹的 没钱滚蛋')

 

  7.练习

# 数据类型转换提示
age = input('age>>>:')
real_age = 18
# 将字符串的数字转换成整型
age = int(age)

    猜年龄的游戏     

      普通要求
        用户可以有三次猜错的机会 如果过程中猜对了直接退出
      拔高要求
        三次机会用完之后提示用户是否继续尝试 如果是则再给三次机会 如果否则直接结束

# 设定谜底18岁
answer = 18
# 设定抽奖次数
freq = 0
# 进入猜谜环节,设置规则
while freq < 3:
    # 客户输入
    age = input('猜个年龄:')
    # 将字符串的数字转换成整型
    age = int(age)
    # 对比抽奖结果对不对
    if age == answer:
        print('恭喜猜中500万')
        break
    else:
        print('很遗憾')
        freq += 1


# 设定谜底18岁
answer = 18
# 设定一轮抽奖次数
freq = 0
# 设顶最多玩几次
freq_1 = 0
# 进入猜谜环节,设置规则
while freq < 3:
    # 客户输入
    age = input('猜个年龄:')
    # 每输入一次就记一次
    freq += 1
    freq_1 += 1
    # 将字符串的数字转换成整型
    age = int(age)
    # 对比抽奖结果对不对
    if age == answer:  # 对了直接中奖
        print('恭喜猜中500万!!!')
        break
    else:
        print('很遗憾')
        if freq == 3 and freq_1 < 6:  # 当第三次,总次数也没有到第7次
            desire = input('你是否要再玩三次:')  # 问一下需要再玩3次吗
            if not desire == '':  # 只要不否  就可以再玩三次
                freq = 0  # 当前一轮次数归0
            else:  # 否则直接结束
                break

 

For循环

  1.for单独使用的时候

    for循环能做到的事情 while循环都可以做到
    但是for循环语法更加简洁 并且在循环取值问题上更加方便

name_list = ['jason', 'tony', 'kevin', 'jack', 'xxx']
# 循环取出列表的每一个元素并打印
# while实现
# count = 0
while count < 5:
    print(name_list[count])
    count += 1
# for循环
for name in name_list:
    print(name)

    

    for 变量名 in 可迭代对象: # 字符串、列表、字典、元组、集合
      for循环体代码

    ps:变量名如果没有合适的名称 那么可以使用i,j,k,v,item

name_list = ['jason', 'tony', 'kevin', 'jack', 'xxx']
# 循环取出列表的每一个元素并打印
# while实现
count = 0
while count < 5:
    print(name_list[count])
    count += 1
# for循环
for name in name_list:
    print(name)

 

  2.for+break

    break功能也是用于结束本层循环

for i in range(10):
    if i == 4:
        break
    print(i)

 

  3.for+continue

    continue功能也是用于结束本次循环

for i in range(10):
    if i == 4:
        continue
    print(i)

 

  4.for+else

    else也是在for循环正常结束的情况下才会执行

for i in range(10):
    if i == 4:
        break
    print(i)
else:
    print('你追我!!!')

 

  5.for循环的嵌套使用

for i in range(3):
    for j in range(5):
        print("*", end='')
    print()


for i in range(1, 10):
    for j in range(1, i + 1):
        print('%s*%s=%s' % (i, j, i * j), end=' ')
    print()

 

  6.习题

    输出金字塔

      方法一:

#     *      #第1行打印4个空格+1个*
#    ***     #第2行打印3个空格+3个*
#   *****    #第3行打印2个空格+5个*
#  *******   #第4行打印1个空格+7个*
# *********  #第5行打印0个空格+9个*
cengshu = input('请输入金字塔层数:')  # 输入的层数
cengshu = int(cengshu)  # 总层数
print(' '*4+'*'*1)  # 1层
print(' '*3+'*'*3)  # 2层
print(' '*2+'*'*5)  # 3层
print(' '*1+'*'*7)  # 4层
print(' '*0+'*'*9)  # 5层

      方法二:

cengshu = input('请输入金字塔层数:')  # 输入的层数
cengshu = int(cengshu)  # 总层数
ceng = 1
for i in range(1,cengshu+1):
    print(' '*(cengshu - ceng) + '*'*(2*ceng - 1))  # 总层数 - 层数 = 空格数 层数*2 - 1= *数
    ceng += 1  # 层数 + 1

      方法三:

cengshu = input('请输入金字塔层数:')  # 输入的层数
cengshu = int(cengshu)  # 总层数转化成整型
for ceng in range(1,cengshu+1):  # 循环一层一层往上
    for j in range(cengshu - ceng):  # 循环(cengshu - ceng)次,每次取一个' ',取(cengshu - ceng)个一轮
        print(' ', end='')
    for k in range(2*ceng - 1):  # 循环(2*ceng - 1)次,每次取一个'*',取(2*ceng - 1)个一轮
        print('*', end='')
    print()  # 一轮后转行

 

 

END

 

posted @ 2021-11-05 19:06  Snails蜗牛  阅读(69)  评论(0)    收藏  举报