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

高级问题

酷兔AI论文
酷兔AI论文

专业原创高质量、低查重,免费论文大纲,在线AI生成原创论文,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





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

相关专题

更多
while的用法
while的用法

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

91

2023.09.25

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

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

168

2023.11.13

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

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

10

2025.11.08

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

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

27

2025.12.06

Java编译相关教程合集
Java编译相关教程合集

本专题整合了Java编译相关教程,阅读专题下面的文章了解更多详细内容。

9

2026.01.21

C++多线程相关合集
C++多线程相关合集

本专题整合了C++多线程相关教程,阅读专题下面的的文章了解更多详细内容。

3

2026.01.21

无人机驾驶证报考 uom民用无人机综合管理平台官网
无人机驾驶证报考 uom民用无人机综合管理平台官网

无人机驾驶证(CAAC执照)报考需年满16周岁,初中以上学历,身体健康(矫正视力1.0以上,无严重疾病),且无犯罪记录。个人需通过民航局授权的训练机构报名,经理论(法规、原理)、模拟飞行、实操(GPS/姿态模式)及地面站训练后考试合格,通常15-25天拿证。

15

2026.01.21

Python多线程合集
Python多线程合集

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

1

2026.01.21

java多线程相关教程合集
java多线程相关教程合集

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

3

2026.01.21

热门下载

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

精品课程

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

共21课时 | 2.8万人学习

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

共8课时 | 1.5万人学习

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

共0课时 | 0人学习

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

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