在python中实现ZeroDivisionError异常



所以,在编程方面,我是一个彻头彻尾的菜鸟,周末才开始学习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 语句,而是尝试这样做

print('You can't divide by 0!!') # or similar

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

问题是你在 for 循环中尝试/排除。 如果您不希望程序在遇到异常时终止,只需在打印警告后中断循环即可。

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:
            print ("Cannot divide by zero")
            break

您可以尝试引发异常 检查以下示例

#!/usr/bin/env python3
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]) 
        except ZeroDivisionError as err: # issue with implementing this exception
            raise Exception("Cannot divide by zero", err)

你怎么称呼回答自己的问题? 所以我尝试了一件我以前没有的事情,而不是通过我使用了中断语句和 vuala。 接下来,我将尝试将其结果写入一个简单的.txt文件中,这是到目前为止为任何感兴趣的人提供的代码

# 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: # in case of variable z = 0
            print('You shoudln't divide by 0!!')
            break
    break

您可以在进入循环之前验证用户输入的除数。然后,您无需检查 for 循环中的除以零错误:

while True:
    try:
        x = int(input('From: '))
        y = int(input('.. to: '))
        z = int(input('.. that can be divided by: '))
        if z == 0:
            print('You can't divide by 0!!')
            continue            
    except ValueError: # in case of a nonint input
        print('You should enter a valid number!')
        continue
    for a in range(x, y):
        if a % z == 0:
            print(a, [a / z], end = ' ')
    print()

在您的代码中,捕获ZeroDivisionError的唯一方法是变量z的内容为 0。在这一点上,我注意到您的代码中存在一些问题:

  1. 在将 x 和 y 传递给range()函数之前,应确保x < y
  2. 您应该始终确保z不为 null,然后脚本将只检查它是否可以以您想要的方式划分。

这是我的主张:

# 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
    numbers = range(x, y)
    if len(numbers) > 0 and z != 0:  #Check if the list is empty and if the divider is not null
        for number in numbers:
            if number % z == 0:
                print(number, [number / z])
    else:
        print("Invalid numbers passed")`

相关内容

  • 没有找到相关文章

最新更新