在python中实现ZeroDivisionError异常

在python中实现ZeroDivisionError异常,python,pycharm,divide-by-zero,Python,Pycharm,Divide By Zero,所以,说到编程,我是一个彻头彻尾的傻瓜,只是在周末才开始学习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

所以,说到编程,我是一个彻头彻尾的傻瓜,只是在周末才开始学习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)次。有什么建议吗?非常感谢您可以尝试在下面的示例中引发异常检查

#!/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)

您可以尝试在下面的示例中引发异常检查

#!/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)

问题是您正在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

问题是您正在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

你如何回答自己的问题?所以我尝试了一件我以前没有尝试过的事情,我用了break语句和vuala来代替pass。接下来,我将尝试将结果写入一个简单的.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

你如何回答自己的问题?所以我尝试了一件我以前没有尝试过的事情,我用了break语句和vuala来代替pass。接下来,我将尝试将结果写入一个简单的.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()

您可以在进入循环之前验证用户输入的除数。然后,不需要检查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。此时,我注意到您的代码中存在一些问题:

  • 在将x和y传递给
    range()
    函数之前,应确保
    x
  • 您应该始终确保
    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")`
    

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

  • 在将x和y传递给
    range()
    函数之前,应确保
    x
  • 您应该始终确保
    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")`
    


    如果您希望程序在被零除事件时终止,最好按照@varad的建议引发一个异常。情况并非如此。我希望继续循环,直到用户输入正确的值gotcha。我的答案提供了您正在寻找的功能。如果您希望程序在被零除的事件中终止,最好按照@varad的建议引发一个异常。情况并非如此,我希望继续循环,直到用户输入正确的值gotcha。我的答案提供了您正在寻找的功能。我已经尝试了您建议的方案,它工作正常,但其他方式似乎都可以。。。什么都没有,程序以代码0终止,没有返回值…我已经尝试了您建议的方案,它工作正常,但它似乎以其他方式工作。。。没事,程序以代码0终止,不返回任何值…如果您想以异常方式解决它,为什么不简单地让
    ZeroDivisionError
    传播?对不起,我想我忘了提到我的母语不是英语。什么意思?@Varad谢谢你的建议,我运行了你的示例并终止了程序,这不是我的目标。@MichalFoc我想你不应该
    引发异常。对不起,我误解了你的问题。如果是这样的话,你可以给出一个
    print
    语句,然后使用
    break
    for
    循环中出来,返回
    while
    。如果你想解决这个问题,除了一个例外,为什么不简单地让
    zeroditionerror
    传播呢?对不起,我想我忘了提到我的母语不是英语。什么意思?@Varad谢谢你的建议,我运行了你的示例并终止了程序,这不是我的目标。@MichalFoc我想你不应该
    引发异常。对不起,我误解了你的问题。如果是这样的话,你可以给出一个
    print
    语句,然后使用
    break
    for
    循环中出来,转到
    while
    。谢谢Vinny,我喜欢你的想法,第1点肯定会像我用x>y测试它时一样实现,结果为0(虽然程序以代码0终止…),但是我认为有一个教育性的位是很好的,在这里你可以温和地通知/提醒用户你不应该除以0:)至于检查列表是否为空,我认为它是用除ValueError处理的。谢谢你的输入Hanks Vinny,我喜欢你的想法,第1点肯定会在我用x>y测试时实现,结果为0(虽然程序以代码0终止…),但是我觉得有一点教育意义很好,您可以温和地通知/提醒用户您不应该除以0:)至于检查列表是否为空,我认为它是用except ValueError处理的。谢谢你的意见