Invalid syntax in while loop

2091 views python
9

I've been trying to make a program in python that checks matching length of the first element of the list with the other elements. If the length and sum of digits match with the first element, that corresponding element is printed.

Here's the program I wrote..

            n=int(input())
            l=[]
            t=0
            u=0
            for i in range(0,n):
              p=int(input())
              l.append(p)
            q=l[0]
            print(q)
            for j in range(1,n):
              if(len(l[0])==len(l[j])):
                summ=0
                sum1=0
                while(q>0):
                  {
                    t=q%10
                    summ=summ+t
                    q=q/10;
                  }
                while(l[j]>0):
                  {
                    u=l[j]%10;
                    sum1=sum1+u
                    l[j]=l[j]/10
                  }
                if(summ=sum1):
                  print(l[j])
                else:
                  continue

When I try to run this, it says invalid syntax at the part t=q%10, tried tweaking it and then it showed an error on the next line summ=summ+t. A bit confused here.

answered question

Hmm.. '{' and '}' seriously, do you need them in Python?

Silly mistake yeah

1 Answer

7

You have the syntax wrong, python doesn't use {} brackets to delineate blocks, it uses indentation. Just remove the brackets.

posted this

Have an answer?

JD

Please login first before posting an answer.