美文网首页
【14】python第十三---while循环

【14】python第十三---while循环

作者: 咗嚛 | 来源:发表于2020-03-10 22:18 被阅读0次

    '''
    while条件:
    条件成立要重复执行的代码
    '''

    需求:重复打印5次老婆,我错啦 ---1, 2, 3, 4, 5 6---数字表示循环的次数 -- 第一次是1 最后依次5

    1 + 1+

    i = 0
    while i < 5:
    print('老婆,我错啦')
    i += 1
    print('我原谅你了')

    分析 1-100 累加的合

    '''
    1.列出1-100的数值,增量为1
    2.准备保存 打印结果
    3.循环做加法运算
    4.打印结果
    5.验证结果
    '''
    i = 1
    result = 0
    while i <= 100:
    #前两个数的结果+第三个数的结果
    result += i
    i += 1
    print(result)

    偶数相加

    i = 0
    result = 0
    while i <= 100:
    result += i
    i +=2
    print(result)

    通过取偶数的值进行验证

    i = 1
    result = 0
    while i <= 100:
    if i % 2 ==0:
    result += i
    i +=1
    print(result)

    相关文章

      网友评论

          本文标题:【14】python第十三---while循环

          本文链接:https://www.haomeiwen.com/subject/enmndhtx.html