list1 = ["5", "-", "2", " ", "1"]
int_list = []
while len(list1) > 0:
if len(int_list) < 1:
int_list.append(int(list1[0]))
list1.pop(0)
int_list.append(0)
int_list.append(int(list1[1]))
int_list.pop(0)
int_list.pop(0)
print(int_list)
I'm trying to make calculator. But this code doesn't execute. Like no errors no texts just blank. Anyone knows why this happening?
CodePudding user response:
There are some errors (just mentioned in earlier post), so I would not repeat.
It seems that you are starting to implement some stack data structure to do some math evaluation. Great! Here is some suggestions and code snippet for you to start.
First, try to use meaningful but succinct variable names. Secondly, write some pseudo code in paper and flowchart, then finally implement it. The chance of success at first time is much much better..
L = ["5", "-", "2", " ", "1"]
eval_lst = list()
for i, x in enumerate(L):
try:
eval_lst.append(int(x))
except ValueError:
eval_lst.append(x)
CodePudding user response:
The condition len(list1) > 0
will always be True
cause you are not removing any item from list1
.
To do this you need to replace
list1.pop(0)
-> del list1[0]
and
int_list.pop(0)
-> del int_list[0]
You have to use del
to remove the item at a specific index