0

Can someboody give me solution of this python exercise ?

Write a program which takes 2 digits, X,Y as input and generates a 2-dimensional array. The element value in the i-th row and j-th column of the array should be i*j.

12th Jun 2017, 6:11 PM
Masquerade
Masquerade - avatar
1 Respuesta
+ 5
result = [] # create an empty list (array name in Python) X = int(input('Enter the number of rows: ')) Y = int(input('Enter the number of columns: ')) for i in range(X): result.append([]) # create an empty list for row at index i for j in range(Y): result[i].append(str((i+1)*(j+1))) # store the value i*j (using one start count, rather than index zero start count to avoid entire first column and first row being filled with zeros) # output the resulting 2d array in a simply and friendly formatted way s = '' for i in range(X): s += ', '.join(result[i])+'\n' print(s)
12th Jun 2017, 7:27 PM
visph
visph - avatar