Am currently working on a function that takes in a list. Each element of this list is a list of integers. This list will then be converted to a list of dictionaries. Where the values in each nested list will be multiplied before being outputted as shown below. All list that contain less than 2 integers will also be skipped. So far I am able to get the desired output using a test list but have run into 2 issues: 1st issue: Desired output:
test_list = [[1,3,3], [2,5,-1],[3,2],[4,5,3],[0,23],[1,2,3,4], [1]]
[{'qns': '1 x 3 x 3', 'ans': 9},
{'qns': '2 x 5 x -1', 'ans': -10},
{'qns': '3 x 2', 'ans': 6},
{'qns': '4 x 5 x 3', 'ans': 60},
{'qns': '0 x 23', 'ans': 0},
{'qns': '1 x 2 x 3 x 4', 'ans': 24}]
Am currently getting this output:
test_list = [[1,3,3], [2,5,-1],[3,2],[4,5,3],[0,23],[1,2,3,4], [1]]
[{'qns': '2 x 5 x -1', 'ans': -10}, {'qns': '3 x 2', 'ans': 6}, {'qns': '4 x 5 x 3', 'ans': 60}, {'qns': '0 x 23', 'ans': 0}, {'qns': '1 x 2 x 3 x 4', 'ans': 24}]
2nd issue: May I know how I can add user input functionality to obtain the numbers for the test_list.
[Current Code]
def generate_qns_from_list(lst):
Qns_List=[]
for sumList in lst:
if(len(sumList)>=2):
question=""
question+=str(sumList[0])
answer=sumList[0]
for i in range(1,len(sumList)):
question+=" x "+str(sumList[i])
answer*=sumList[i]
subDict={'qns':question,'ans':answer}
Qns_List.append(subDict)
return Qns_List
input_list=[[1,3,3],[2,5,-1],[3,2],[4,5,3],[0,23],[1,2,3,4],[1]]
print(generate_qns_from_list(input_list))