I am looking to make a program where the user enters a sum eg. 5 + 6 - 7, and my program recognise's it and gives the answer eg 4. So far my program will take the input, recoginises the numbers and operations, and assigns them to a list, however, I am not sure how to take the numbers and operators and calculate them. Does anyone have anything that could help??
Here is my code
s = "5 + 6 - 7" #User input placeholder
q = s.split() #splits the input into a list with an element for every character(not spaces)
print(q)
n = [] #numbers in user input
a = [] #actions/operations that the user inputs
print(q)
for x in range(0, len(q)):
if q[x].isdigit(): #determines whether an element is a digit
q[x] = int(q[x]) #str to int
n.append(q[x]) #adds it to the n list
print(n)
else:
a.append(q[x]) #adds an operation to the a list
print(a)
answer = n[0], a[0], n[1], a[1], n[2]
print("Operations are: ", a)
print("Numbers are: ", n)
print("Answer is: ", answer)