#/usr/bin/python2.4 -tt
import sys
def mult_table(x,y):
for i in range(1,y+1):
for n in range(1,x+1):
if n == 1:
print i*n,
elif n == x:
print repr(i*n).rjust(3)
else:
print repr(i*n).rjust(3),
mult_table(12,3)
sys.exit(0)
I submitted this short program to CodeEval. Its only job is to make a multiplication table. The problem is that it should not have any trailing whitespace and when I submitted it the last line has a space after the 36. I tested myself, and I don’t have this problem. What is going on?
Your code adds a space for
mult_table(1,1)not formult_table(12,3).So you need to fix your
if n == 1case (to also checkn == x, as you do later in the code).I would do this a bit differently, so that the heart of
mult_tableis something like:And then just
printyour newline in the outer for loop.Take-home: Test your boundary conditions! 🙂