Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- maxNum = int(input("What number do you want to go up to? ")) + 1
- firstColumnJustifyWidth = len(str(maxNum))
- justifyWidth = firstColumnJustifyWidth**2 # Rough guess at how much we'll need to space things out
- if justifyWidth == 1: # For 0 < x < 9
- justifyWidth += 1
- headerNumbers = ['X'.rjust(firstColumnJustifyWidth)] # Start off with just the 'X'
- for columnNumber in range(1, maxNum): # For each column
- headerNumbers.append(str(columnNumber).rjust(justifyWidth)) # Add all the header numbers into the list...
- print(' '.join(headerNumbers)) # Join the list together with at least one space
- for rowNumber in range(1, maxNum): # For each row...
- formattedRowNumber = str(rowNumber).rjust(firstColumnJustifyWidth) # Format the row number for printing
- rowData = []
- for columnNumber in range(1, maxNum):
- rowData.append(str(rowNumber * columnNumber).rjust(justifyWidth)) # For each column: Do the multiplication and format the result for printing
- formattedRowData = ' '.join(rowData) # Join the list together with at least one space
- print(formattedRowNumber + ' ' + formattedRowData)
Advertisement
Add Comment
Please, Sign In to add comment