3

I am trying to create a program that creates a multiplication table of n x n. It's required for the assignment to use repeated addition instead of the multiplication function.

This is the code I have so far:

def main():
import math
print('Hello!')
n = (abs(eval(input("Enter n for the multiplication table n x n: "))))
n = int(n)
a = 0
for i in range(1,n+1):
    for x in range(1,n+1):
        a = i+a
        print(i,' * ',x,' = ',a)
main()

It gives me an output like this:

Hello!
Enter n for the multiplication table n x n: 4
1  *  1  =  1
1  *  2  =  2
1  *  3  =  3
1  *  4  =  4
2  *  1  =  6
2  *  2  =  8
2  *  3  =  10
2  *  4  =  12
3  *  1  =  15
3  *  2  =  18
3  *  3  =  21
3  *  4  =  24
4  *  1  =  28
4  *  2  =  32
4  *  3  =  36
4  *  4  =  40

The output is obviously incorrect, so what can I change/add to fix the calculations?

1
  • Please please please, do not use eval(input(...)). Use int(input() in this case. Commented Mar 5, 2017 at 20:37

3 Answers 3

5

You have a variable a inside of your nested for loops that you continuously add values to for different values of the multiplication table. Instead of adding i to a each iteration, let a = i*x. This will give you the correct value of multiplication each time. However, if you really want to do it with repeated addition, set a = 0 outside of the second for loop, but inside the first, like so:

for i in range(1,n+1):
    for x in range(1,n+1):
        a = i+a
        print(i,' * ',x,' = ',a)
    a = 0
Sign up to request clarification or add additional context in comments.

2 Comments

I would suggest to use .format in this case, it makes things cleaner: print("{}*{} = {}".format(i, x, acc))
That was what I was missing. Thank you so much!
0

For the print statement try using instead:

print(i,' * ',x,' = ',i*x)

I'm not certain what you are using the 'a' variable for, but if you'd like to output the multiplication of i and x still using a, instead keep your code the same and just change what you have for a in your nest for loop:

a = i*x

Hope this helps!

2 Comments

That worked, but for the assignment, I can't use the multiplication function, i have to use repeated addition.
Ah, if that's the case I'd follow UnknowableIneffible's solution, as he addressed how to solve the problem with repeated addition.
0

In your for loop, you're always incrementing the variable 'a' by itself added to 'i' each time, instead you should multiply i*x

print("Hello!")

n = input("Enter n for the multiplication table n x n: ")
n = int(n)

result = 0

for i in range(1,n+1):
    for j in range(1, n+1):
        result = i*j
        print(i," * ", j, " = ", result)

1 Comment

Why do you have result = 0? It is no longer necessary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.