0

Please help me to figure it out what is wrong with this code! It's the last lesson of data structures course 🙏

We have to print True or False if the input expression is equilibrated or not (parentheses equilibrated) Here's my code: def balanced(expression): #tu código va aquí items=expression.split() for i in range(len(items)): par= [] if i== "(": par.insert(0,"(") elif i==")": if par!= []: par.pop(0) else: return("False") if par== []: print("True") else: print("False") print(balanced(input()))

22nd Aug 2022, 10:24 PM
Mabel Zavala Moreira
2 ответов
+ 2
Well, firstly, remove the extra print function, otherwise you essentially get print(print()). Secondly, what is the value of the variable "i"? And thirdly, "par==[]" will always be True 😉
22nd Aug 2022, 10:52 PM
Solo
Solo - avatar
0
Mabel Zavala Moreira If you print function then return in function otherwise it would print 'None' Define empty list outside the loop Change your loop like because no need to split string for i in expression: Don't compare list like that. Get length of list then compare with 0 which means list is empty. ------Solution----- def balanced(expression): #tu código va aquí items = expression.split() par = [] #for i in range(len(items)): for i in expression: #par = [] if i == "(": par.insert(0, "(") elif i == ")": #if par != []: if len(par) > 0: par.pop(0) else: return False #if par == []: return len(par) == 0 print(balanced(input()))
23rd Aug 2022, 2:58 AM
A͢J
A͢J - avatar