Python之while循环语句

1、最简单的while循环语句

count = 0
while True:
    count = count +1
    print(count)

2、加上条件判断循环

age = 23
count = 0
while count < 3:
    age_guess = int(input("请输入数字:"))
    if age_guess == age :
        print("你猜对了啦")
        break;
    elif age_guess > age :
        print("你猜大了")
    else:
        print("你猜小了")
    count = count+1

print("游戏结束")

3、while循环另类用法(else)

age = 23
count = 0
while count < 3:
    age_guess = int(input("请输入数字:"))
    if age_guess == age :
        print("你猜对了啦")
        break;
    elif age_guess > age :
        print("你猜大了")
    else:
        print("你猜小了")
    count = count+1
else:                   #当while判断不成立时,运行else语句
    print("你输入的次数太多啦")     

print("游戏结束")

分享文章:Python之while循环语句
标题网址:http://xwwzsj.com/article/piegjj.html

其他资讯

售后响应及时

7×24小时客服热线

数据备份

更安全、更高效、更稳定

价格公道精准

项目经理精准报价不弄虚作假

合作无风险

重合同讲信誉,无效全额退款