码迷,mamicode.com
首页 > 其他好文 > 详细

while循环练习

时间:2020-05-30 23:27:11      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:div   用户登录   span   input   pre   错误   输出   password   输入   

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

while循环练习

标签:div   用户登录   span   input   pre   错误   输出   password   输入   

原文地址:https://www.cnblogs.com/Yangyl00/p/12995602.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!