Define plus_table ():
For I( 1, 10) in the range:? # Set the multiplicand i, which is also the line number. The first line does not print blank lines, and other lines will wrap when the multiplicand changes.
If I> 1:? # Blank lines are not printed in the first line, and the line breaks when the multipliers of other businesses change.
Print ()
For j( 1, i+1) in the range:? # Set the multiplier, which is also the column number. Each multiplicand formula needs to be in the same column, so it cannot be printed by default. Need to add the default parameter end is empty.
print("%d X %d = %-2d " % (i,j,i * j),end= " ")? # Because the result has the possibility of 1 digit and 2 digits, for the sake of alignment, the result is set to %-2d, that is, the space of 2 digits is reserved and aligned to the left.
Now Baidu has no function of inserting code. See figure for clear indentation.
The implementation results are as follows:
If you want to be consistent with the common multiplication table, you can exchange the I and J positions of the last row, as shown below:
print("%d X %d = %-2d " % (j,I,i * j),end= " ")
The implementation results are as follows: