What's wrong in this code? (Read Description) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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))

12th Aug 2023, 6:47 PM
Salman Iqbal
Salman Iqbal - avatar
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.
12th Aug 2023, 8:30 PM
Dan
+ 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
12th Aug 2023, 8:32 PM
Per Bratthammar
Per Bratthammar - avatar