python中zerodivisionerror_在python中实现ZeroDivisionError异常

这篇博客记录了一位初学者尝试编写Python程序的过程,该程序旨在让用户输入一个范围,然后找出这个范围内所有能被指定数字整除的数。作者遇到了错误处理的问题,特别是对于除以零的情况,并且寻求改进代码的建议。
摘要由CSDN通过智能技术生成

所以,在编程方面我是个十足的傻瓜,上周末才开始学习python。无论如何,作为Youtube教程中的一个挑战,我本来打算写一个程序,可以找到定义范围内的所有数字,当除以4时,会给我0提示,然后它们就会被列出。所以我把它放得更远一点,下面是我目前所得到的。在# Program that let's you find all numbers in any range defined by user

# that can be divided without any reminder by the number also defined by user

print('Find all numbers in range...')

while True:

try:

x = int(input('From: ')) # begining of the range

y = int(input('.. to: ')) # end of the range

z = int(input('.. that can be divided by: ')) # value of the number to divide by

except ValueError: # in case of a nonint input

print('You should enter a valid number!')

continue

for a in range(x, y):

try:

if a % z == 0:

print(a, [a / z], end = ' ')

except ZeroDivisionError: # issue with implementing this exception

pass

为了避免使用pass语句,我尝试

^{pr2}$

它打印字符串(x,y)次。有什么建议吗?非常感谢

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值