I've made a small function to output a multiplication grid (those things from primary school! :P) to an inputted number.
def make_grid(max):
max += 1 # so the for loops go up to the desired number, inclusive
print('X\t' + '\t'.join([str(_) for _ in range(max)]) + '\n') # print all the numbers hoizontally
for i in range(max):
print(i, end="\t") # print the vertical column of base numbers
for x in range(max):
print(str(i*x), end="\t") # multiply the two numbers
print('\n')
if __name__ == "__main__":
try:
max = int(input("Max Number? "))
make_grid(max)
except ValueError:
print("Please enter a number!")
Choosing a max number of 5 (make_grid(5)
) would output:
X 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 1 2 3 4 5 2 0 2 4 6 8 10 3 0 3 6 9 12 15 4 0 4 8 12 16 20 5 0 5 10 15 20 25
I'm looking for any improvements I could make, especially the \t
's - as I use them to line up the numbers, but the gap gets pretty big, unnecessarily.