mình đang tự học python và đang code thử thuật toán balan ngược và đang bị lỗi ở vòng lặp while. Lỗi mình gặp phải là vừa xong vòng lặp while trong 1 hàm đó thì thay vì chỉ kết thúc vòng lặp while tự nhiên nó thoát ra khỏi cái hàm luôn trong khi chưa đủ điều kiện để thoát. Xin mọi ng chỉ giáo, cảm ơn
`def change(a): # chuyển chuỗi về từng cụm
base=[]
pos=0
for i in range(0,len(a)):
if a[i]<'0' or a[i]>'9':
#print a[pos:i]
base.insert(len(base),a[pos:i])
base.insert(len(base), a[i])
pos=i+1
base.insert(len(base),a[pos:len(a)])
return base
def compare(a,b):# so sánh các toán tử
if a=='+' or a=='-':
if b=='+' or b=='-':
return 0
else:
return -1
else:
if b=='+' or b=='-':
return 1
else:
return 0
def balan(a):# chuyển về dạng hậu tố
result=[]
temp=[]
for i in range(0,len(a)):
if a[i][0]<'9' and a[i][0]>'0':
result.insert(len(result),a[i])
else:
if len(temp)==0:
temp.insert(len(temp),a[i])
else:
while compare(a[i],temp[len(temp)-1])>=0 and len(temp)>0:# nơi xảy ra lỗi
result.insert(len(result),temp[len(temp)-1])
del temp[len(temp)-1]
temp.insert(len(temp),a[i])
return result
a=change("12+23-23-22*98")
b=balan(a)`