python - I am trying to get an upper triangle in a matrices, but i did not get an expected output. Could everyone help me out -
# timestable.py t = int(input("what size of multiplication table see (enter 1 – 12)?")) if (t <=12): s = input("what kind table display, enter either r, u, or l? (r regular full matrix, u upper triangular matrix, l lower triangular matrix)?") if s=="r": row in range (1,t+1): col in range (1,t+1): prod = row * col if prod < 10: print(' ', end = '') print(row * col, ' ', end = '') print() elif s== "l" : in range(1,t+1): j in range (1, i+1): prod = *j if prod < 10: print(' ', end = '') print(i * j, ' ', end = '') print() elif s== "u" : in range(1,t+1): j in range (1, i-1): prod = *j if prod < 10: print(' ', end = '') print(j * i, ' ', end = '') print() else : t= int(input("please enter valid multiplecation table size (enter 1 – 12)?"))
if want upper(lower) triangular matrix know element x @ position i,j > j (i < j) 0. suggest use condition and, regarding output, use tab.
# upper example t = 6 in range(1, t+1): j in range (1, t+1): if > j: print("0\t", end=' ') # use whitespace if want instead of 0 else: prod = i*j print("%s\t" % prod, end=' ') print()
output
1 2 3 4 5 6 0 4 6 8 10 12 0 0 9 12 15 18 0 0 0 16 20 24 0 0 0 0 25 30 0 0 0 0 0 36
Comments
Post a Comment