日间循环练习
这里有一些针对数字的 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
- 数字总和 编写一个程序来计算给定数字的数字之和(例如,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
高级问题
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
以上是日间循环练习的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

Python适合数据科学、Web开发和自动化任务,而C 适用于系统编程、游戏开发和嵌入式系统。 Python以简洁和强大的生态系统着称,C 则以高性能和底层控制能力闻名。

2小时内可以学会Python的基本编程概念和技能。1.学习变量和数据类型,2.掌握控制流(条件语句和循环),3.理解函数的定义和使用,4.通过简单示例和代码片段快速上手Python编程。

Python在游戏和GUI开发中表现出色。1)游戏开发使用Pygame,提供绘图、音频等功能,适合创建2D游戏。2)GUI开发可选择Tkinter或PyQt,Tkinter简单易用,PyQt功能丰富,适合专业开发。

两小时内可以学到Python的基础知识。1.学习变量和数据类型,2.掌握控制结构如if语句和循环,3.了解函数的定义和使用。这些将帮助你开始编写简单的Python程序。

Python更易学且易用,C 则更强大但复杂。1.Python语法简洁,适合初学者,动态类型和自动内存管理使其易用,但可能导致运行时错误。2.C 提供低级控制和高级特性,适合高性能应用,但学习门槛高,需手动管理内存和类型安全。

要在有限的时间内最大化学习Python的效率,可以使用Python的datetime、time和schedule模块。1.datetime模块用于记录和规划学习时间。2.time模块帮助设置学习和休息时间。3.schedule模块自动化安排每周学习任务。

Python在web开发、数据科学、机器学习、自动化和脚本编写等领域有广泛应用。1)在web开发中,Django和Flask框架简化了开发过程。2)数据科学和机器学习领域,NumPy、Pandas、Scikit-learn和TensorFlow库提供了强大支持。3)自动化和脚本编写方面,Python适用于自动化测试和系统管理等任务。

Python在自动化、脚本编写和任务管理中表现出色。1)自动化:通过标准库如os、shutil实现文件备份。2)脚本编写:使用psutil库监控系统资源。3)任务管理:利用schedule库调度任务。Python的易用性和丰富库支持使其在这些领域中成为首选工具。
