Python While 循环教程 #Day3
本篇文章主要是结合我之前面试的各种经历和实战开发中遇到的问题解决经验整理的,希望这篇《Python While 循环教程 #Day3》对你有很大帮助!欢迎收藏,分享给更多的需要的朋友学习~

This Python code implements several functions using while loops to solve various number-related problems:
1. Armstrong Number: An Armstrong number is a number that is equal to the sum of its own digits each raised to the power of the number of digits. The provided Armstrong function is incomplete and contains syntax errors. A corrected version is shown below.
2. Neon Number: A neon number is a number where the sum of the digits of its square is equal to the original number. The code correctly identifies neon numbers.
3. Factorial: The code calculates the factorial of a given number.
4. Perfect Number: A perfect number is a positive integer that is equal to the sum of its proper divisors (excluding itself). The code accurately identifies perfect numbers.
5. Prime Number: The code checks if a number is prime.
6. Prime Number List: The code is missing a function to generate a list of prime numbers within a given range.
7. Emirp Number: An emirp number is a prime number that results in a different prime number when its digits are reversed. The code correctly identifies emirp numbers.
Here's the corrected and improved code:
# Armstrong Number
def Armstrong(Num):
num_str = str(Num)
num_digits = len(num_str)
sum_of_powers = 0
for digit in num_str:
sum_of_powers += int(digit) ** num_digits
return sum_of_powers == Num
# Example usage
number = 153
if Armstrong(number):
print(f"{number} is an Armstrong number")
else:
print(f"{number} is not an Armstrong number")
# Neon Number
Num = int(input("Enter the number: "))
Square = Num * Num
Sum = 0
while Square > 0:
Sum += Square % 10
Square //= 10
if Sum == Num:
print(f"{Num} is a Neon Number")
else:
print(f"{Num} is not a Neon Number")
# Factorial of a Number
Num = int(input('Enter the number: '))
Fact = 1
i = 1
while i <= Num:
Fact *= i
i += 1
print('Factorial:', Fact)
# Perfect Number
Num = int(input('Enter the number: '))
i = 1
sum = 0
while i < Num:
if Num % i == 0:
sum += i
i += 1
if sum == Num:
print(f"{Num} is a Perfect Number")
else:
print(f"{Num} is not a Perfect Number")
# Prime Number
def isPrimeNumber(Num):
if Num <= 1:
return 0
i = 2
while i * i <= Num:
if Num % i == 0:
return 0
i += 1
return 1
# Example usage:
num = int(input("Enter a number to check if it's prime: "))
if isPrimeNumber(num):
print(f"{num} is a prime number")
else:
print(f"{num} is not a prime number")
# Emirp Number
def isEmirp(Num):
if not isPrimeNumber(Num):
return False
rev_num = int(str(Num)[::-1])
return isPrimeNumber(rev_num) and rev_num != Num
# Example usage
num = int(input("Enter a number to check if it's an emirp number: "))
if isEmirp(num):
print(f"{num} is an Emirp number")
else:
print(f"{num} is not an Emirp number")
This revised code is more efficient, readable, and error-free. Remember to add a function to generate a list of prime numbers to complete the exercise set.
今天关于《Python While 循环教程 #Day3》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!
简述优化 Linux 内存性能的核心思想
- 上一篇
- 简述优化 Linux 内存性能的核心思想
- 下一篇
- Win11如何开启Direct3D加速-Win11开启3D加速的方法
-
- 文章 · python教程 | 6小时前 |
- Python logging Formatter 统一结构化字段输出
- 185浏览 收藏
-
- 文章 · python教程 | 7小时前 | 配置 · python · Python教程 · 配置管理 Python 3.15 frozendict 不可变映射
- Python 3.15 frozendict 内置类型的配置使用场景
- 192浏览 收藏
-
- 文章 · python教程 | 10小时前 | 序列化 · python · Python pickle 进程池 multiprocessing Pool
- Python multiprocessing 进程池传递不可序列化对象
- 255浏览 收藏
-
- 文章 · python教程 | 11小时前 | 数据一致性 · Python教程 · Python 事务 自动提交 sqlite3 autocommit isolation_level
- Python sqlite3 事务模式与自动提交边界
- 197浏览 收藏
-
- 文章 · python教程 | 12小时前 |
- Python contextlib.nullcontext 统一同步异步入口
- 316浏览 收藏
-
- 文章 · python教程 | 12小时前 | 面向对象 · python · Python教程 · InitVar __post_init__ Python dataclass 派生字段 field(init=False)
- Python dataclass __post_init__ 计算派生字段
- 303浏览 收藏
-
- 文章 · python教程 | 13小时前 |
- Python typing.TypeGuard 处理复杂容器类型收窄
- 437浏览 收藏
-
- 文章 · python教程 | 15小时前 |
- Python os.fspath 支持自定义路径对象
- 214浏览 收藏
-
- 文章 · python教程 | 17小时前 | 异常处理 · 异步编程 · Python教程 · asyncio · 后台任务 任务取消 CancelledError Python asyncio asyncio.shield
- Python asyncio.shield 保护后台任务免受外层取消
- 407浏览 收藏
-
- 文章 · python教程 | 19小时前 |
- Python configparser ExtendedInterpolation 组织分层配置
- 480浏览 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 485次学习
-
- PubMedQA
- 深入了解PubMedQA生物医学问答数据集,涵盖其核心功能、使用方法及在临床决策、药物研发等场景的应用,助力提升NLP模型性能。
- 410次使用
-
- H2O EvalGPT
- H2O EvalGPT是H2O.ai推出的开源LLM评估平台,提供详细的大模型性能排行榜、行业特定基准测试及A/B测试功能,助您快速选择最适合项目的高性能大语言模型。
- 491次使用
-
- LMArena
- LMArena是加州大学伯克利分校推出的AI模型匿名评测平台。通过盲测投票机制,用户可对比不同大模型回答并生成实时排行榜,助力开发者优化模型及用户选择最佳AI工具。
- 498次使用
-
- HELM
- 深入了解斯坦福推出的HELM(Holistic Evaluation of Language Models)大模型评测体系。本文解析其核心功能、安装配置步骤及应用场景,涵盖准确性、公平性、鲁棒性等多维度指标,助力开发者全面优化语言模型性能。
- 447次使用
-
- MMBench
- MMBench是由上海人工智能实验室等机构联合推出的多模态基准测试平台,提供细粒度能力评估、大规模数据集及VLMEvalKit工具。本文详细介绍其核心功能、安装使用方法及应用场景,助力开发者全面评估多模态模型性能。
- 275次使用
-
- Python sqlite3 Connection serialize 怎么导出数据库快照:备份窗口、内存占用与恢复校验
- 2026-08-26 501浏览
-
- Python监控网页状态:requests异常处理实战
- 2026-05-29 501浏览
-
- TensorFlow模型部署为API的TF Serving方法
- 2026-05-26 501浏览
-
- Python字符串编码转换:encode与decode详解
- 2026-05-16 501浏览
-
- TensorFlow裁剪无用算子方法详解
- 2026-05-15 501浏览

