0
How do I print a multiplication table neatly?
I want to print a multiplication table from 1 to 5 so it looks like this: 1 x 1 = 1 1 x 2 = 2 ... 5 x 5 = 25 What’s the simplest way to do that with loops?
3 ответов
+ 2
Aleksei Radchenkov provided the basis for a oneliner
print('\n'.join(f"{a} x {b} = {a * b}" for a in range(1, 6) for b in range(1, 6)))
+ 1
Once again, please provide an example of your attempt.
The idea though is to use nested loops (one for a and one for b in a x b = c).
0
BroFar
or a more prettified version
print(*(f" {a:>2} x {b:>2} = {(a * b):>3}{'' if b<10 else '\n'}" for a in range(1, 11) for b in range(1, 11)), sep='\n', end='')