Apr-20-2017, 07:36 PM
I'm not a student anywhere, just doing python exercises to try and get better at coding through practice and sheer determination.
Currently, I am doing 100 python challenges that provide a challenge and the answer. The question I was stuck at stated:
# 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. <--- where the problem losses me
The answer provided, I don't understand it. That's why I am here, to see if you guys could explain how the code provides the answer.
Answer:
Currently, I am doing 100 python challenges that provide a challenge and the answer. The question I was stuck at stated:
# 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. <--- where the problem losses me
The answer provided, I don't understand it. That's why I am here, to see if you guys could explain how the code provides the answer.
Answer:
[list=1]
[*]row_num = int(input("Input number of rows: "))
[*]col_num = int(input("Input number of columns: "))
[*]multi_list = [[0 for col in range(col_num)] for row in range(row_num)]
[*]
[*]for row in range(row_num):
[*] for col in range(col_num):
[*] multi_list[row][col]= row*col
[*]
[*]print(multi_list)
[/list]I was able to rewrite part of the code to:row= int(input("insert row \n: "))
col = int(input("insert col \n: "))
def d_array(r,c):
array = [[i*j for j in range(c)] for i in range(r)]
return array
print(d_array(row,col))Sadly I still don't understand how it works, anything would help even suggested reading material. I really want to understand
