[英]ZeroDivisionError: integer division or modulo by zero
I am building a simple algorithm to find common divisors between two given numbers: 我正在构建一个简单的算法来查找两个给定数字之间的公约数:
i = int(input("digite o 1o inteiro positivo: "))
j = int(input("digite o 2o inteiro positivo: "))
#i,j = 9,6
aux, cont = 1, 0
if i > j: # 9 < 6
for n in range (i+1): # n = (1,2,3,4,5,6,7,8,9)
while n <= i: # (1,2,3,4,5,6,7,8,9)
if i % n == 0 and j % n == 0: # 9 % (1,3,9) e 6 % (1,3,6)
print(n) # print(1,3)
Why is my programm having this ZeroDivisionError
? 为什么我的程序有这个
ZeroDivisionError
?
Start your range()
at 1, not 0 with: 从1开始你的
range()
,而不是0:
for n in range(1, i + 1): # n = (1,2,3,4,5,6,7,8,9)
i, j = 9, 6
if i > j: # 9 < 6
for n in range(1, i + 1): # n = (1,2,3,4,5,6,7,8,9)
if i % n == 0 and j % n == 0: # 9 % (1,3,9) e 6 % (1,3,6)
print(n) # print(1,3)
1
3
I actually just wanted to indent the code in your question, but I ended up accidentally repairing it. 我实际上只是想在你的问题中缩进代码,但我最终意外地修复了它。 So, here is a working solution:
所以,这是一个有效的解决方案:
i = int(input("Give the first positive integer: "))
j = int(input("Give the second positive integer: "))
r = j
if i < j:
r = i
for n in range (2, r + 1):
if i % n == 0 and j % n == 0:
print(n)
Output: 输出:
Give the first positive integer: 27
Give the second positive integer: 18
3
9
The range starts with 2
, because 1
is a unit, and therefore not interesting as divisor (it divides everything anyway). 范围从
2
开始,因为1
是一个单位,因此作为除数没有意义(它无论如何都会划分所有内容)。 The 0
should not be checked, because it does not divide anything, and because it caused Div-by-zero errors. 不应该检查
0
,因为它不会划分任何东西,因为它会导致Div-by-zero错误。
Whatever... Refreshed some python... 无论如何......刷新一些蟒蛇......
Your loop starts with 0
so this ZeroDivisionError
occurs. 你的循环从
0
开始,所以出现ZeroDivisionError
。
exception ZeroDivisionError Raised when the second argument of a division or modulo operation is zero.
exception ZeroDivisionError当除法或模运算的第二个参数为零时引发。 The associated value is a string indicating the type of the operands and the operation.
关联值是一个字符串,表示操作数的类型和操作。 [source]
[资源]
You have to start your for loop from 1
你必须从
1
开始你的for循环
like this: for n in range(1,i+1):
像这样:
for n in range(1,i+1):
and you don't have to do while loop
this will go infinite. 而且你不必做
while loop
这将是无限的。
your code will be: 你的代码将是:
i = int(input("digite o 1o inteiro positivo: "))
j = int(input("digite o 2o inteiro positivo: "))
# i,j = 9,6
aux, cont = 1, 0
if i > j: # 9 < 6
for n in range (1,i+1): # n = (1,2,3,4,5,6,7,8,9)
#while n <= i: # (1,2,3,4,5,6,7,8,9)
if i % n == 0 and j % n == 0: # 9 % (1,3,9) e 6 % (1,3,6)
print(n) # print(1,3)
or you can also write your code in try-except
block, like this which will give the same output: 或者您也可以在
try-except
块中编写代码,这样会产生相同的输出:
i = int(input("digite o 1o inteiro positivo: "))
j = int(input("digite o 2o inteiro positivo: "))
# i,j = 9,6
aux, cont = 1, 0
if i > j: # 9 < 6
for n in range (i+1): # n = (1,2,3,4,5,6,7,8,9)
try:
if i % n == 0 and j % n == 0: # 9 % (1,3,9) e 6 % (1,3,6)
print(n) # print(1,3)
except ZeroDivisionError:
n+= 1
digite o 1o inteiro positivo: 9
digite o 2o inteiro positivo: 6
1
3
Function range() can have up to 3 arguments: 函数range()最多可以包含3个参数:
range(initial limit included, final limit not included, step) 范围(包括初始限制,不包括最终限制,步骤)
If you use for x in range(y) with only one argument, and such case is equal to: for y in range(0, x, 1) # including: [0, 1, ..., x - 1] 如果仅使用一个参数用于范围(y)中的x,并且此类情况等于:对于范围(0,x,1)中的y,包括:[0,1,...,x - 1]
If you need to start your range with 1, you need to define it: for y in range(1, y) 如果你需要用1开始你的范围,你需要定义它:y在范围内(1,y)
If you need to change range step, you also need to define it: for y in range(0, -50, -1) 如果需要更改范围步长,还需要定义它:对于范围内的y(0,-50,-1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.