Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...