while循环

## while循环
基本语法:while 条件为true
            
            代码块

while 1:
    egon_age = 73

    age = input('请输入你猜的年龄》》》')
    age = int(age)

    if age == egon_age:
        print('猜对了')
        
     
    elif age > egon_age:
        print('猜大了')
    elif age < egon_age:
        print('猜小了')
        
      
       
请输入你猜的年龄》》》 23


猜小了
while 1:
    egon_age = 73

    age = input('请输入你猜的年龄》》》')  # 73
    age = int(age)  # 73

    if age == egon_age:  # 成立
        print('猜对了')
        break
    elif age > egon_age:
        print('猜大了')
    elif age < egon_age:
        print('猜小了')
请输入你猜的年龄》》》 74


猜大了


请输入你猜的年龄》》》 73


猜对了

上方为while+break

while循环的嵌套

prize={0:'小红花',1:'大红花',2:'飞吻'}
while True:
    egon_age=23
    age=input('请输入您的猜测:')
    age=int(age)
    if age==egon_age:
        while True:
            print(f'猜对了,请选择您的礼物:{prize}')
        choice=input('请选择你的礼物:')
        choice=int(choice)
        if choice==2:
            print('不给\n请重新选择')
        else:
            print('给你')
            break
        break
    elif age>egon_age:
        print('大了')
    elif age<egon_age:
        print('小了')

while+continue

count = 1
while count < 101: # 1<101,2<101,count=50,
    # if count != 50:
    #     print(count)  # 1,2
    # count += 1  # count=2,count=3

    if count == 50:
        count += 1
        print('用不着')
        continue   # 不执行下面的代码

    print(count)  # 1,2
    count += 1  # count=2,count=3
break直接终止整个while循环,
continue是终止本次循环,直接下一次循环。
posted @ 2024-01-12 19:33  泥跟沃丸吗  阅读(10)  评论(0编辑  收藏  举报