My favorite projects programming is writing a multiplication table. I do it for most languages. It incorporates loops, formatting, and if else statements
Here is the complete source code
header_txt = "\tmultiplication table and 2d array"
x = header_txt.expandtabs(49)
print(x)
for x in range(1, 16):
if x==1:
print(" ",x, end="\t")
else:
print("\t",x, end="\t")
#horizontal space
print()
print("_______________________________________________________________________________________________________________________________")
for y in range(1, 16):
for z in range(1, 16):
if z == 1:
print("\t", y, end="\t")
if (y * z)<=99:
print("\t", (y) * (z), end="\t")
else:
print(" ", (y) * (z), end="\t")
print("\n")
Output of the project
Below is the title of the project.
for x in range (1, 16)
first number of the range is 1. The last is all numbers inside the range. So the last number is 15. expandtabs command centered the header.
if x==1 print statements moves the top row of numbers over to line up properly.
header_txt = "\tmultiplication table and 2d array"
x = header_txt.expandtabs(49)
print(x)
for x in range(1, 16):
if x==1:
print(" ",x, end="\t")
else:
print("\t",x, end="\t")
#horizontal space
print()
print("_______________________________________________________________________________________________________________________________")
_____________________________________________________________________________________________________________________________")
Below is the part of the project that creates the table. tabs don’t work great in python so I had to get a bit creative. The numbers under 99 lined up perfectly. I needed to fix those above 99.
if (y * z) <=99, the result was tabbed over on both sides. if (Y * z) is greater than 99, I used a space on the left and a tab on the right to line up those numbers.
for y in range(1, 16):
for z in range(1, 16):
if z == 1:
print("\t", y, end="\t")
if (y * z)<=99:
print("\t", (y) * (z), end="\t")
else:
print(" ", (y) * (z), end="\t")
print("\n")