zoukankan      html  css  js  c++  java
  • python-while循环的练习题

    # !/use/bin/env python
    # -*-conding:utf-8-*-

    #author:shanshan

    """
    写代码
    a. 使用 while 循环实现输出 1,2,3,4,5, 7,8,9, 11,12
    b. 使用while 循环输出100-50,从大到小,如1009998…,到50时再从0循环输出到50,然后结束
    c. 使用 while 循环实现输出 1-100 内的所有奇数
    d. 使用 while 循环实现输出 1-100 内的所有偶数
    e. 使用while循环实现输出2-3+4-5+6…+100 的和
    """

    #a. 使用 while 循环实现输出 1,2,3,4,5, 7,8,9, 11,12
    i = 0
    while i <=13:
    if i == 6 and i == 10:
    break
    else:
    print(i)
    i = i+1


    #b. 使用while 循环输出100-50,从大到小,如1009998…,到50时再从0循环输出到50,然后结束
    count = 100
    list_100_to_small = []
    list_50_to_small = []
    while count>0:
    if count>=50:
    list_100_to_small.append(count)
    else:
    list_50_to_small.append(count)
    #list_50_to_small.reverse()
    count -=1
    list_0_big = list_50_to_small[::-1]
    list_100_to_small.extend(list_0_big)
    print(list_100_to_small)

    #c. 使用 while 循环实现输出 1-100 内的所有奇数
    count = 1
    while count <= 100:
    if count % 2 == 1:
    print(count)
    count +=1

    #d. 使用 while 循环实现输出 1-100 内的所有偶数
    count = 1
    while count <= 100:
    if count % 2 == 0:
    print(count)
    count +=1

    #e. 使用while循环实现输出2-3+4-5+6…+100 的和
    count = 2
    sum = 0
    while count <= 100:
    if count % 2 == 1:
    sum_count = -count
    else:
    sum_count = count
    sum = sum_count+sum
    count +=1
    print(sum)
  • 相关阅读:
    构建之法阅读笔记2
    2020.2.10
    2020.2.9
    2020.2.8
    2020.2.7
    2020.2.6
    2020.2.5
    学习进度——第十三周
    PHPStorm+XAMPP开发php项目
    第一阶段成果建议
  • 原文地址:https://www.cnblogs.com/shanshan-test/p/12527039.html
Copyright © 2011-2022 走看看