0
What's wrong in this code? (Read Description)
Below is code that I wrote for a practice quiz in 'Python Data Structures' course. It worked and met all possibilities except one. And that was one of the locked ones so I couldn't see what was the issue. # Code for checking if every opened bracket is closed in an input expression (True) or not (False). def balanced(a): #your code goes here o = 0 for x in a: if x == "(": o += 1 if x == ")": o -= 1 return o == 0 a = input() print(balanced(a))
2 Answers
+ 1
I donât have access to that course to see how the question is posed but I donât see why it wouldnât work. The only case I can think of where this would not technically be correct is if there is if ) appears before (. You could check a condition where o < 0 and return false in that case.
+ 1
Hi, Salman Iqbal !
You have to modify the code. For example this input comes True:
)(5)(
Thatâs not correct.
You can take a look at this code if you want to have some code to compare with:
https://code.sololearn.com/cpuSHXo64jsS/?ref=app