while循环练习
Posted yangyl00
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
以上是关于while循环练习的主要内容,如果未能解决你的问题,请参考以下文章