日间循环练习

日间循环练习

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

基本问题

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

def print_number(no):    num=1    while num<=no:        print(num, end=" ")        num+=1print_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 totalno=int(input("sum of the number:"))print(sum_of_number(no))
sum of the number:1055

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

def print_even_number(no):    num=2    while num<=no:        print(num, end=" ")        num+=2print_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 nono=int(input("enter the number:"))print_odd_number(no)
enter the number:201 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-=1print_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 factorialnum=int(input("enter the number:"))print(find_factorial(num))
enter the number:5120

数字总和编写一个程序来计算给定数字的数字之和(例如,123 → 1 2 3=6)。

def sum_of_digits(num):    sum=0    while num>0:        sum=sum+num%10        num=num//10    return sumnum=int(input("enter the number:"))print(sum_of_digits(num))
enter the number:1236

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

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

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

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

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

def multiply(num):    no=1    while no<=15:        print(no,"*",num,"=",no*num , end="n")        no+=1num=int(input("enter the number:"))multiply(num)
enter the number:121 * 12 = 122 * 12 = 243 * 12 = 364 * 12 = 485 * 12 = 606 * 12 = 727 * 12 = 848 * 12 = 969 * 12 = 10810 * 12 = 12011 * 12 = 13212 * 12 = 14413 * 12 = 15614 * 12 = 16815 * 12 = 180

高级问题

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

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

*2.找到力量*

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

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 countdef find_power(base,power):    result=1    while power>=1:        result=result*base        power-=1    return resultdef find_armstrong(num,count):    armstrong=0    while num>0:        rem=num%10        result= find_power(rem,count)        armstrong=armstrong+result        num=num//10    return armstrongnum=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:123not armstrongenter the number:153armstrong

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,oddnum=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:12345sum of even number: 9sum of odd number: 6

以上就是日间循环练习的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1354337.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年12月13日 18:27:45
下一篇 2025年12月13日 18:27:58

相关推荐

  • HTML、CSS 和 JavaScript 中的简单侧边栏菜单

    构建一个简单的侧边栏菜单是一个很好的主意,它可以为您的网站添加有价值的功能和令人惊叹的外观。 侧边栏菜单对于客户找到不同项目的方式很有用,而不会让他们觉得自己有太多选择,从而创造了简单性和秩序。 今天,我将分享一个简单的 HTML、CSS 和 JavaScript 源代码来创建一个简单的侧边栏菜单。…

    2025年12月24日
    200
  • 前端代码辅助工具:如何选择最可靠的AI工具?

    前端代码辅助工具:可靠性探讨 对于前端工程师来说,在HTML、CSS和JavaScript开发中借助AI工具是司空见惯的事情。然而,并非所有工具都能提供同等的可靠性。 个性化需求 关于哪个AI工具最可靠,这个问题没有一刀切的答案。每个人的使用习惯和项目需求各不相同。以下是一些影响选择的重要因素: 立…

    2025年12月24日
    300
  • 带有 HTML、CSS 和 JavaScript 工具提示的响应式侧边导航栏

    响应式侧边导航栏不仅有助于改善网站的导航,还可以解决整齐放置链接的问题,从而增强用户体验。通过使用工具提示,可以让用户了解每个链接的功能,包括设计紧凑的情况。 在本教程中,我将解释使用 html、css、javascript 创建带有工具提示的响应式侧栏导航的完整代码。 对于那些一直想要一个干净、简…

    2025年12月24日
    000
  • 布局 – CSS 挑战

    您可以在 github 仓库中找到这篇文章中的所有代码。 您可以在这里查看视觉效果: 固定导航 – 布局 – codesandbox两列 – 布局 – codesandbox三列 – 布局 – codesandbox圣杯 &#8…

    2025年12月24日
    000
  • 隐藏元素 – CSS 挑战

    您可以在 github 仓库中找到这篇文章中的所有代码。 您可以在此处查看隐藏元素的视觉效果 – codesandbox 隐藏元素 hiding elements hiding elements hiding elements hiding elements hiding element…

    2025年12月24日
    400
  • 居中 – CSS 挑战

    您可以在 github 仓库中找到这篇文章中的所有代码。 您可以在此处查看垂直中心 – codesandbox 和水平中心的视觉效果。 通过 css 居中 垂直居中 centering centering centering centering centering centering立即…

    2025年12月24日 好文分享
    300
  • 如何在 Laravel 框架中轻松集成微信支付和支付宝支付?

    如何用 laravel 框架集成微信支付和支付宝支付 问题:如何在 laravel 框架中集成微信支付和支付宝支付? 回答: 建议使用 easywechat 的 laravel 版,easywechat 是一个由腾讯工程师开发的高质量微信开放平台 sdk,已被广泛地应用于许多 laravel 项目中…

    2025年12月24日
    000
  • 如何在移动端实现子 div 在父 div 内任意滑动查看?

    如何在移动端中实现让子 div 在父 div 内任意滑动查看 在移动端开发中,有时我们需要让子 div 在父 div 内任意滑动查看。然而,使用滚动条无法实现负值移动,因此需要采用其他方法。 解决方案: 使用绝对布局(absolute)或相对布局(relative):将子 div 设置为绝对或相对定…

    2025年12月24日
    000
  • 移动端嵌套 DIV 中子 DIV 如何水平滑动?

    移动端嵌套 DIV 中子 DIV 滑动 在移动端开发中,遇到这样的问题:当子 DIV 的高度小于父 DIV 时,无法在父 DIV 中水平滚动子 DIV。 无限画布 要实现子 DIV 在父 DIV 中任意滑动,需要创建一个无限画布。使用滚动无法达到负值,因此需要使用其他方法。 相对定位 一种方法是将子…

    2025年12月24日
    000
  • 移动端项目中,如何消除rem字体大小计算带来的CSS扭曲?

    移动端项目中消除rem字体大小计算带来的css扭曲 在移动端项目中,使用rem计算根节点字体大小可以实现自适应布局。但是,此方法可能会导致页面打开时出现css扭曲,这是因为页面内容在根节点字体大小赋值后重新渲染造成的。 解决方案: 要避免这种情况,将计算根节点字体大小的js脚本移动到页面的最前面,即…

    2025年12月24日
    000
  • Nuxt 移动端项目中 rem 计算导致 CSS 变形,如何解决?

    Nuxt 移动端项目中解决 rem 计算导致 CSS 变形 在 Nuxt 移动端项目中使用 rem 计算根节点字体大小时,可能会遇到一个问题:页面内容在字体大小发生变化时会重绘,导致 CSS 变形。 解决方案: 可将计算根节点字体大小的 JS 代码块置于页面最前端的 标签内,确保在其他资源加载之前执…

    2025年12月24日
    200
  • Nuxt 移动端项目使用 rem 计算字体大小导致页面变形,如何解决?

    rem 计算导致移动端页面变形的解决方法 在 nuxt 移动端项目中使用 rem 计算根节点字体大小时,页面会发生内容重绘,导致页面打开时出现样式变形。如何避免这种现象? 解决方案: 移动根节点字体大小计算代码到页面顶部,即 head 中。 原理: flexível.js 也遇到了类似问题,它的解决…

    2025年12月24日
    000
  • 形状 – CSS 挑战

    您可以在 github 仓库中找到这篇文章中的所有代码。 您可以在此处查看 codesandbox 的视觉效果。 通过css绘制各种形状 如何在 css 中绘制正方形、梯形、三角形、异形三角形、扇形、圆形、半圆、固定宽高比、0.5px 线? shapes 0.5px line .square { w…

    2025年12月24日
    000
  • 有哪些美观的开源数字大屏驾驶舱框架?

    开源数字大屏驾驶舱框架推荐 问题:有哪些美观的开源数字大屏驾驶舱框架? 答案: 资源包 [弗若恩智能大屏驾驶舱开发资源包](https://www.fanruan.com/resource/152) 软件 [弗若恩报表 – 数字大屏可视化组件](https://www.fanruan.c…

    2025年12月24日
    000
  • 网站底部如何实现飘彩带效果?

    网站底部飘彩带效果的 js 库实现 许多网站都会在特殊节日或活动中添加一些趣味性的视觉效果,例如点击按钮后散发的五彩缤纷的彩带。对于一个特定的网站来说,其飘彩带效果的实现方式可能有以下几个方面: 以 https://dub.sh/ 网站为例,它底部按钮点击后的彩带效果是由 javascript 库实…

    2025年12月24日
    000
  • 网站彩带效果背后是哪个JS库?

    网站彩带效果背后是哪个js库? 当你访问某些网站时,点击按钮后,屏幕上会飘出五颜六色的彩带,营造出庆祝的氛围。这些效果是通过使用javascript库实现的。 问题: 哪个javascript库能够实现网站上点击按钮散发彩带的效果? 答案: 根据给定网站的源代码分析: 可以发现,该网站使用了以下js…

    好文分享 2025年12月24日
    100
  • 产品预览卡项目

    这个项目最初是来自 Frontend Mentor 的挑战,旨在使用 HTML 和 CSS 创建响应式产品预览卡。最初的任务是设计一张具有视觉吸引力和功能性的产品卡,能够无缝适应各种屏幕尺寸。这涉及使用 CSS 媒体查询来确保布局在不同设备上保持一致且用户友好。产品卡包含产品图像、标签、标题、描述和…

    2025年12月24日
    100
  • 如何利用 echarts-gl 绘制带发光的 3D 图表?

    如何绘制带发光的 3d 图表,类似于 echarts 中的示例? 为了实现类似的 3d 图表效果,需要引入 echarts-gl 库:https://github.com/ecomfe/echarts-gl。 echarts-gl 专用于在 webgl 环境中渲染 3d 图形。它提供了各种 3d 图…

    2025年12月24日
    000
  • 如何在 Element UI 的 el-rate 组件中实现 5 颗星 5 分制与百分制之间的转换?

    如何在el-rate中将5颗星5分制的分值显示为5颗星百分制? 要实现该效果,只需使用 el-rate 组件的 allow-half 属性。在设置 allow-half 属性后,获得的结果乘以 20 即可得到0-100之间的百分制分数。如下所示: score = score * 20; 动态显示鼠标…

    2025年12月24日
    100
  • CSS 最佳实践:后端程序员重温 CSS 时常见的三个疑问?

    CSS 最佳实践:提升代码质量 作为后端程序员,在重温 CSS/HTML 时,你可能会遇到一些关于最佳实践的问题。以下将解答三个常见问题,帮助你编写更规范、清晰的 CSS 代码。 1. margin 设置策略 当相邻元素都设置了 margin 时,通常情况下应为上一个元素设置 margin-bott…

    2025年12月24日
    000

发表回复

登录后才能评论
关注微信