• while循环练习


    1、使用while循环输入1 2 3 4 5 6    8 9  10

    num = 1
    while num <= 10:
        if num == 7:
            print(" ")
        else:
            print(num)
        num = num + 1

    2、求1-100的所有数的和

    num = 1
    he = 0
    while num <= 100:
        he = he + num
        num = num +1
    print("1-100的和为:",he)

    3、输出1-100内的所有奇数

    num = 1
    he = 0
    while num <= 100:
        if (num%2) != 0:
            he = he + num
        num = num + 1
    print("1-100内所有奇数和:",he)

    4、输出1-100内的所有偶数

    num = 1
    he = 0
    while num <= 100:
        if (num%2) == 0:
            he = he + num
        num = num + 1
    print("1-100内所有偶数和:",he)

    5、求1+2+3.....+99+100的所有数的和

    num = 1
    he = 0
    while num <= 100:
        if (num%2) != 0:
            he = he + num
        else:
            he = he - num
        num = num +1
    print("1-2+3-4+5...+33-100的和为:",he)

    6、用户登录(三次机会)

    num = 1
    while num <= 3:
        name = input("请输入用户名:")
        password = input("请输入密码:")
        if name == "张三" and password == "111111":
            print("欢迎")
            break
        else:
            if num != 3:
                print("用户名或密码错误,请重新登录")
            else:
                print("用户名或密码输入错误超过三次,请1分钟后再试")
        num = num +1
  • 相关阅读:
    无题..
    让Windows 2003 Server支持ASP程序
    下雪
    比较经典的.NET基础
    XML几种操作
    某年某月有几天
    .NET操作Word(傻瓜型)
    XML 简单操作
    一首歌
    ASP.NET:掌握Web窗体的生命周期与状态管理(摘自网络)
  • 原文地址:https://www.cnblogs.com/Yangyl00/p/12995602.html
Copyright © 2020-2023  润新知