Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

python - getting ZeroDivisionError: integer division or modulo by zero

I had written a simple pascal triangle code in python but I am getting a error

def factorial(n):
    c=1
    re=1
    for c in range(n):
        re = re * c;
    return(re)

print "Enter how many rows of pascal triangle u want to show 
"
n=input();
i=1
c=1
for i in range(n):
    for c in range(n-i-1):
        print ""
        for c in range(i):
            a = factorial(i);
            b = factorial(c);
            d = factorial(i-c);
            z = (a/(b*d));
            print "%d" % z
            print "
"

ERROR:

Traceback (most recent call last):
  File "/home/tanmaya/workspace/abc/a.py", line 19, in <module>
    z = (a/(b*d));
ZeroDivisionError: integer division or modulo by zero
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

ZeroDivisionError means that you were trying to divide or modulo, a number n with 0.
in your case, z = (a/(b*d)) resulted in z = (a/0)

Also, as @theB pointed out, your factorial function is incorrect.

Try fixing those.

Also, you don't really need ; in your code. It's usually the case we put ; when we want to make the code one liner.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...