| """ |
| 循环10次 你好 |
| """ |
| i = 1 |
| while i <= 20: |
| print("你好") |
| i += 1 |
| print("程序结束") |
| |
| """ |
| 循环10次 你好,并显示循环的次数 |
| """ |
| i = 1 |
| while i <= 20: |
| print(f"你好{i}") |
| i += 1 |
| print("程序结束") |
| |
| """ |
| 循环输出1-20之间的偶数,能被2整除的数就是偶数 |
| """ |
| i = 1 |
| while i <= 20: |
| if i % 2 == 0: |
| print(i) |
| i += 1 |
| |
| """ |
| 循环输出1-20之间的偶数,能被2整除的数就是偶数 |
| """ |
| i = 2 |
| while i <= 20: |
| print(i) |
| i += 2 |
| |
| """ |
| 求1-10的和 |
| """ |
| s = 0 |
| i = 1 |
| while i <= 10: |
| s = s + i |
| i += 1 |
| print(s) |
| |
| """ |
| 4. 编写一个程序,要求用户输入一个正整数,然后使用`while`循环判断这个数是否为质数。 |
| 质数: 只能被1和他自身整除的数, |
| 方式1: 这个数n分别去除以1...n ,统计能被整除的数个数。 |
| 如果个数=2 就是质数,否则就不是质数 |
| 方式2: 这个数n分别去除以2....n-1,如果发现,中间有数能被n整除,直接退出。 |
| """ |
| c = 0 |
| i = 1 |
| n = int(input("请输入一个正整数:")) |
| while i <= n: |
| if n % i == 0: |
| c += 1 |
| i += 1 |
| if c == 2: |
| print(f"{n}是质数") |
| else: |
| print(f"{n}不是质数") |
| """ |
| 立即结束当前循环,break |
| 输出1-10的数,遇到3就退出 |
| """ |
| i = 1 |
| while i <= 10: |
| print(f"i={i}") |
| if i == 3: |
| break |
| i += 1 |
| |
| """ |
| 遇到continue,立即结束本次循环,进入下一次循环 |
| 输出1-10,3或者5不显示 |
| """ |
| i = 1 |
| while i <= 10: |
| if i == 3 or i == 5: |
| i += 1 |
| continue |
| print(i) |
| i += 1 |
| """ |
| 计算1-100的偶数 |
| """ |
| i = 0 |
| sum = 0 |
| while i <= 100: |
| sum += i |
| i += 2 |
| print(sum) |
| |
| """ |
| 找出整数的因子(能被该整数整除) |
| """ |
| i = int(input("输入整数")) |
| j = 1 |
| while j <= i: |
| if i%j == 0: |
| print(j) |
| j += 1 |
| print("结束") |
| |
| """ |
| 打印斐波那契数列前20个数 |
| 1,1,2,3,5,8,13 |
| """ |
| i = 1 |
| s = 1 |
| a = 0 |
| b = 1 |
| print(s) |
| while i<=19: |
| print(s) |
| a = b |
| b = s |
| s = a+b |
| i+=1 |
| |
| """ |
| 编写一个程序,使用while循环,从1开始往上数,打印出第一个能被7整除的数。 |
| """ |
| i = 1 |
| while i>0: |
| if i%7 == 0: |
| print(i) |
| break |
| else: |
| i+=1 |
| |
| """ |
| 编写一个程序,计算出1到100之间所有能被3整除的数的和。 |
| """ |
| i = 1 |
| s = 0 |
| while i<=100: |
| if i%3 == 0: |
| s += i |
| i+=1 |
| else: |
| i+=1 |
| print(s) |
| |
| """ |
| 编写一个程序,统计100以内个位数是2并且能够被3整除的数的个数。 |
| """ |
| i = 1 |
| s = 0 |
| while i<=100: |
| if i%10 == 2 and i%3 == 0: |
| print(i) |
| s += 1 |
| i+=1 |
| else: |
| i+=1 |
| print(s) |
| |
| """ |
| 编写一个程序,输入任意一个正整数,求它是几位数 |
| """ |
| i = int(input("输入正整数")) |
| s = 0 |
| while i != 0: |
| i = i // 10 |
| s += 1 |
| print(s) |
| |
| """ |
| 编写一个程序,输入任意一个正整数,求每一位相加的和 (123 = 1+2+3= 6) |
| """ |
| i = int(input("输入正整数")) |
| j = 0 |
| while i != 0: |
| j += i % 10 |
| i = i // 10 |
| print(j) |
| |
| """ |
| 打印1000以内所有的水仙花数 |
| (水仙花数是三位数,其各位数字立方和等于该数本身) |
| """ |
| a = 100 |
| while a <= 1000: |
| i = a |
| j = 0 |
| s = 0 |
| while i != 0: |
| j = i % 10 |
| s += j * j * j |
| i = i // 10 |
| if s == a: |
| print(a) |
| a += 1 |
| |
| """ |
| 编写一个程序,使用while循环,计算并打印出1到100之间所有奇数的和。 |
| 用 continue完成 |
| 不用continue完成 |
| """ |
| i = 0 |
| s = 0 |
| while i < 100: |
| i += 1 |
| if i%2 == 0: |
| continue |
| s += i |
| print(s) |
| |
| i = 1 |
| s = 0 |
| while i <= 100: |
| if i%2 != 0: |
| s += i |
| i += 1 |
| print(s) |
| |
| """ |
| 编写一个程序,使用while循环,计算并打印出1到100之间所有能被3整除但不能被5整除的数。 |
| 用continue完成 |
| 不用continue完成 |
| """ |
| i = 0 |
| while i < 100: |
| i += 1 |
| if i%3 != 0 or i%5 == 0: |
| continue |
| print(i) |
| |
| i = 0 |
| while i <= 100: |
| if i%3 == 0 and i%5 != 0: |
| print(i) |
| i += 1 |
| |
| """ |
| 编写一个程序,使用while循循环,接收用户输入的数字,如果用户输入的数字为0,结束循环,打印出用户输入的所有数字的和。 |
| """ |
| s = 0 |
| while 1 > 0: |
| i = int(input("输入数字:")) |
| if i == 0: |
| break |
| s += i |
| print(s) |
| |
| """ |
| 编写一个程序,判断给定的整数n是否为素数 |
| """ |
| i = int(input("输入数:")) |
| a = 1 |
| s = 0 |
| while a <= i: |
| if i%a == 0: |
| s += 1 |
| a += 1 |
| if s > 2: |
| print("不是素数") |
| else: |
| print("是素数") |
| |
| """ |
| 用户输入账号密码,如果输入不正确从新输入,直到输入正确为止。(账号密码都为:admin) |
| """ |
| i = 1 |
| while i > 0: |
| name = input("请输入用户名:") |
| pwd = input("请输入密码:") |
| if name == "admin" and pwd == "admin": |
| print("输入正确") |
| i = 0 |
| else: |
| print("用户名或密码错误,请重新输入") |
| |
| """ |
| 用户输入账号密码,如果输入不正确从新输入,输入3次错误,就提示账号锁定并结束。(账号密码都为:admin) |
| """ |
| i = 1 |
| s = 0 |
| while i > 0: |
| if s < 3: |
| name = input("请输入用户名:") |
| pwd = input("请输入密码:") |
| if name == "admin" and pwd == "admin": |
| print("输入正确") |
| i = 0 |
| else: |
| s += 1 |
| else: |
| print("输入3次错误,账号锁定") |
| break |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?