I am trying to write the following code: Write a program that accepts a number, n, where -6<n<2. The program will print out the numbers n to n+41 as 6 rows of 7 numbers. The first row will contain the values n to n+6, the second, the values n+7 to n+7+6, and so on.
However, when it comes to a single digit on the first line the alignment goes out. Why does this happen and how can I fix it?
n = int(input("Enter a number between -6 and 2:"))
if n > 2:
print("Invalid input! The value of 'n' should be between -6 and 2.")
elif n <-6:
print("Invalid input! The value of 'n' should be between -6 and 2.")
else:
for i in range(n,n+42,7):
for j in range(i,i + 6):
if -1<j<10:
print(" " + str(j), end = " ")
else:
print(j, sep = " ",end= " ")
print((i+6))
output:
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 32 33 34 35 36
37 38 39 40 41 42 43
It is because you do not correctly space the last column; even though it looks correct for every line after the first. So instead of printing the last number after the inner loop complets, print all the numbers in that loop, and then a blank to terminate the line; as follows: