There are a few issues with your code, but also with your logic. You are increasing a
twice (with for
and addition), b
becomes n-1
the first time through the loop and stays that way, but even if it didn't (eg b = b - 1
), it wouldn't work, because if you are increasing a
and decreasing b
simultaneously, you won't find the correct values unless they happen to match by chance.
Other than that, it's unnecessary to check for a * b != n
, you need to call str
on the integers to add them to strings and the 0 in your range
call is redundant.
whncode's answer is an elegant solution (except for a couple of errors I tried to correct), but to use your logic, you might do this:
for a in range(1, n+1):
for b in range(1, n+1):
if a * b == n:
print str(a) + " times " + str(b) + " equals " + str(n)