I'm new here
I'm having a hard time trying to figure this out and I would really appreciate some advice. I'm actually still learning python basics so please bear with me if its something stupid.
The idea is to provide a list as an input and within the list consisting of other lists. Which I'm then supposed to convert into a question and answer and storing them inside a dictionary. So far this is what I've come up with and I'm currently stuck with accessing the nested list as the first index of each list is a string e.g. ["+", "-", "*", "/"]
I'm currently getting this error:
if (input[x][0]) == "+":
TypeError: list indices must be integers or slices, not list
Here is my code so far:
def math(input):
new_list = []
for x in input:
if (input[x][0]) == "+":
if (input[x][3]):
math = (input[x][1]) + (input[x][2]) + (input[x][3])
str_math = "{1} {0} {2} {0} {3}".format((input[x][0]), (input[x][1]), (input[x][2]), (input[x][3]))
else:
math = (input[x][1]) + (input[x][2])
str_math = "{1} {0} {2}".format((input[x][0]), (input[x][1]), (input[x][2]))
qnsans = {
"qns" : str_math,
"ans" : math
}
new_list.append(qnsans)
return print(new_list)
def main():
input_list = [["+",1,3,3], ["-",2,5,-1]]
math(input_list)
if __name__ == '__main__':
main()
Would appreciate some advice on this thank you!
EDIT: Hi guys, thanks for the responses and it was all due to a blunder on my end for not researching enough on lists and for loops. I appreciate the responses and i'll keep improving on it.