0

0

日间循环练习

花韻仙語

花韻仙語

发布时间:2024-11-28 08:13:43

|

988人浏览过

|

来源于dev.to

转载

日间循环练习

这里有一些针对数字的 while 循环问题供练习:

基本问题

1.打印数字
编写一个程序,使用 while 循环打印从 1 到 10 的数字。

def print_number(no):
    num=1
    while num<=no:
        print(num, end=" ")
        num+=1
print_number(10)

1 2 3 4 5 6 7 8 9 10

2.n 个数字的和
编写一个程序,使用 while 循环计算前 nn 个自然数的和。

def sum_of_number(no):
    num=1
    total=0
    while num<=no:
        total=total+num
        num+=1
    return total
no=int(input("sum of the number:"))
print(sum_of_number(no))
sum of the number:10
55

3.偶数
编写一个程序,使用 while 循环打印 1 到 50 之间的所有偶数。

def print_even_number(no):
    num=2
    while num<=no:
        print(num, end=" ")
        num+=2
print_even_number(50)

2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 

4.奇数
编写一个程序来打印 1 到 nn 之间的所有奇数。

def print_odd_number(no):
    num=1
    while num<=no:
        print(num, end=" ")
        num+=2
    return no
no=int(input("enter the number:"))
print_odd_number(no)
enter the number:20
1 3 5 7 9 11 13 15 17 19

5.倒数
编写一个程序,使用 while 循环以相反的顺序打印从 20 到 1 的数字。

def print_reverse_number(no):
    num=20
    while num>=no:
        print(num, end=" ")
        num-=1
print_reverse_number(1)
20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 

中级问题

1.阶乘计算
编写一个程序,使用 while 循环计算给定数字的阶乘。

def find_factorial(num):
    no=1
    factorial=1
    while no<=num:
        factorial=factorial*no
        no+=1
    return factorial
num=int(input("enter the number:"))
print(find_factorial(num))
enter the number:5
120
  1. 数字总和 编写一个程序来计算给定数字的数字之和(例如,123 → 1 2 3=6)。
def sum_of_digits(num):
    sum=0
    while num>0:
        sum=sum+num%10
        num=num//10
    return sum
num=int(input("enter the number:"))
print(sum_of_digits(num))
enter the number:123
6

3.数数字
编写一个程序来计算给定数字的位数(例如,12345 → 5 位数字)。

def count_of_digits(num):
    count=0
    while num>0:
        num=num//10
        count+=1
    return count
num=int(input("enter the number:"))
print(count_of_digits(num))

enter the number:12345
5

4.反转数字
编写一个程序来反转给定的数字(例如 123 → 321)。

def reverse_number(num):
    reverse=0
    while num>0:
        reverse=reverse*10+num%10
        num=num//10
    return reverse
num=int(input("enter the number:"))
print(reverse_number(num))
enter the number:123
321

5.乘法表
编写一个程序,使用 while 循环打印给定数字 nn 的乘法表。

def multiply(num):
    no=1
    while no<=15:
        print(no,"*",num,"=",no*num , end="\n")
        no+=1
num=int(input("enter the number:"))
multiply(num)
enter the number:12
1 * 12 = 12
2 * 12 = 24
3 * 12 = 36
4 * 12 = 48
5 * 12 = 60
6 * 12 = 72
7 * 12 = 84
8 * 12 = 96
9 * 12 = 108
10 * 12 = 120
11 * 12 = 132
12 * 12 = 144
13 * 12 = 156
14 * 12 = 168
15 * 12 = 180

高级问题

ImgGood
ImgGood

免费在线AI照片编辑器

下载

1.检查回文
编写一个程序来检查给定的数字是否是回文(例如,121→回文,123→不是回文)。

def palindrome(num):
    count=0
    while num>0:
        count=count*10+num%10
        num=num//10
    return count
num=int(input("enter the number:"))
result=palindrome(num)
if result==num:
    print("palindrome")
else:
    print("not palindrome")

enter the number:121
palindrome
enter the number:123
not palindrome

*2.找到力量*

def find_power(base,power):
    result=1
    while power>=1:
        result=result*base
        power-=1
    return result
base=int(input("enter the base number:"))
power=int(input("enter the power number:"))
result=find_power(base,power)
print(result)

enter the base number:2
enter the power number:5
32

3.阿姆斯特朗数
编写一个程序来检查给定的数字是否是阿姆斯特朗数(例如,153 → 13 53 33=15313 53 33=153)。

def count_of_digits(num):
    count=0
    while num>0:
        num=num//10
        count+=1
    return count

def find_power(base,power):
    result=1
    while power>=1:
        result=result*base
        power-=1
    return result

def find_armstrong(num,count):
    armstrong=0
    while num>0:
        rem=num%10
        result= find_power(rem,count)
        armstrong=armstrong+result
        num=num//10
    return armstrong


num=int(input("enter the number:"))
count=count_of_digits(num)
armstrong_result=find_armstrong(num,count)

if armstrong_result==num:
    print("armstrong")
else:
    print("not armstrong")
enter the number:123
not armstrong
enter the number:153
armstrong

4.偶数和奇数位置之和:

def sum_of_even_odd(num):
    odd=0
    even=0
    index=0
    while index<len(num):
        digit=int(num[index])
        if index%2==0:
            even=even+digit

        else:
            odd=odd+digit

        index+=1
    return even,odd

num=input("enter the number:")
even,odd=sum_of_even_odd(num)
print("sum of even number:",even)
print("sum of odd number:",odd) 

Enter the number:12345
sum of even number: 9
sum of odd number: 6

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

WorkBuddy
WorkBuddy

腾讯云推出的AI原生桌面智能体工作台

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
while的用法
while的用法

while的用法是“while 条件: 代码块”,条件是一个表达式,当条件为真时,执行代码块,然后再次判断条件是否为真,如果为真则继续执行代码块,直到条件为假为止。本专题为大家提供while相关的文章、下载、课程内容,供大家免费下载体验。

107

2023.09.25

python如何计算数的阶乘
python如何计算数的阶乘

方法:1、使用循环;2、使用递归;3、使用math模块;4、使用reduce函数。更多详细python如何计算数的阶乘的内容,可以阅读下面的文章。

177

2023.11.13

python求阶乘教程大全
python求阶乘教程大全

本专题整合了python求阶乘相关教程,阅读专题下面的文章了解更多详细内容。

13

2025.11.08

python语言求阶乘
python语言求阶乘

本专题整合了python中阶乘相关教程,阅读专题下面的文章了解更多详细步骤。

43

2025.12.06

TypeScript类型系统进阶与大型前端项目实践
TypeScript类型系统进阶与大型前端项目实践

本专题围绕 TypeScript 在大型前端项目中的应用展开,深入讲解类型系统设计与工程化开发方法。内容包括泛型与高级类型、类型推断机制、声明文件编写、模块化结构设计以及代码规范管理。通过真实项目案例分析,帮助开发者构建类型安全、结构清晰、易维护的前端工程体系,提高团队协作效率与代码质量。

49

2026.03.13

Python异步编程与Asyncio高并发应用实践
Python异步编程与Asyncio高并发应用实践

本专题围绕 Python 异步编程模型展开,深入讲解 Asyncio 框架的核心原理与应用实践。内容包括事件循环机制、协程任务调度、异步 IO 处理以及并发任务管理策略。通过构建高并发网络请求与异步数据处理案例,帮助开发者掌握 Python 在高并发场景中的高效开发方法,并提升系统资源利用率与整体运行性能。

88

2026.03.12

C# ASP.NET Core微服务架构与API网关实践
C# ASP.NET Core微服务架构与API网关实践

本专题围绕 C# 在现代后端架构中的微服务实践展开,系统讲解基于 ASP.NET Core 构建可扩展服务体系的核心方法。内容涵盖服务拆分策略、RESTful API 设计、服务间通信、API 网关统一入口管理以及服务治理机制。通过真实项目案例,帮助开发者掌握构建高可用微服务系统的关键技术,提高系统的可扩展性与维护效率。

272

2026.03.11

Go高并发任务调度与Goroutine池化实践
Go高并发任务调度与Goroutine池化实践

本专题围绕 Go 语言在高并发任务处理场景中的实践展开,系统讲解 Goroutine 调度模型、Channel 通信机制以及并发控制策略。内容包括任务队列设计、Goroutine 池化管理、资源限制控制以及并发任务的性能优化方法。通过实际案例演示,帮助开发者构建稳定高效的 Go 并发任务处理系统,提高系统在高负载环境下的处理能力与稳定性。

59

2026.03.10

Kotlin Android模块化架构与组件化开发实践
Kotlin Android模块化架构与组件化开发实践

本专题围绕 Kotlin 在 Android 应用开发中的架构实践展开,重点讲解模块化设计与组件化开发的实现思路。内容包括项目模块拆分策略、公共组件封装、依赖管理优化、路由通信机制以及大型项目的工程化管理方法。通过真实项目案例分析,帮助开发者构建结构清晰、易扩展且维护成本低的 Android 应用架构体系,提升团队协作效率与项目迭代速度。

99

2026.03.09

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Git 教程
Git 教程

共21课时 | 4.2万人学习

Git版本控制工具
Git版本控制工具

共8课时 | 1.6万人学习

Git中文开发手册
Git中文开发手册

共0课时 | 94人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号