Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Python
list=[]
print(''' press 1 for entering no. into stack
          press 2 for deleting no. from stack  
          press 3 for viewing last no. in stack
          press 4 for viewing entire stack
''')
a=int(input("enter your choice: "))
if a==1:
       b=int(input("enter no. of elements you wanna insert in stack: "))
       for i in range(b):
             c=int(input("enter element " + str(i+1) + ": "))
             list.append(c)
print(list)
print()
elif a==2:
       e=int(input("enter no. of elements you wanna delete from stack: "))
       if len(list)==0:
           print("empty stack: ")
       elif e < len(list):
           print("insufficient elements in list to delete: ")
       else:
           for i in range(e):
               f=list.pop()
               print("popped element is: ", f)
               print("list after popping element :", list)
           print()
           print("final list after popping:", list)
print()
elif a==3:
        if len(list) == 0:
           print("empty stack: ")
           print()
        else:
           print("last element in stack is : ", list[-1])
           print()
elif a==4:
        if len(list) == 0:
           print("empty stack: ")
           print()
        else:
            print("list is: ", list)
            print()
elif a==5:
        print("u have exited......")
        print()
        break;
else:
        print("invalid input")

__________________________________________


ERROR:

 elif a==2:
       ^
SyntaxError: invalid syntax


What I have tried:

I looked it up on google and checked the syntax but couldnt resolve the issue. Your help will be highly beneficial for me and would be much appreciated.
Posted
Updated 26-Jul-22 21:36pm
v2

the elif block must immediately follow an if (or another elif) one. For example, this is allowed
Python
if i<5:
  print("i is less than 5")
elif i<20:
  print("i is between 5 and 19")
else:
  print("i greater thn 19")

This is allowed as well
Python
if i<5:
  print("i is less than 5")
  print(i)
elif i<20:
  print("i is between 5 and 19")
else:
  print("i greater thn 19")

This is NOT allowed
Python
if i<5:
  print("i is less than 5")
print(i) # the if block has completed before this statement
elif i<20: # this elif doesn't follow an if block
  print("i is between 5 and 19")
else:
  print("i greater thn 19")
because, when execution reached the print(i) statement (note its indentation), the if block has already completed.
 
Share this answer
 
Comments
Patrice T 27-Jul-22 3:49am    
5
CPallini 27-Jul-22 5:36am    
Thank you.
akshaychauhan0101 27-Jul-22 5:33am    
Thank you, CPallini. Much appreciated!
CPallini 27-Jul-22 5:37am    
You are welcome.
To add to what CPallini has - rightly - said, in Python indentation is very important - it controls what is and isn't in a block of code.
A simple block of code must all have the indentation the same: the first line that has less spaces before it ends the block.

So in your code:
Python
if a==1:
       b=int(input("enter no. of elements you wanna insert in stack: "))
       for i in range(b):
             c=int(input("enter element " + str(i+1) + ": "))
             list.append(c)
print(list)
print()
elif a==2:
Teh two print statements end the if block, and the following elif has no if to match up with.
Indent it, and it works again:
Python
if a==1:
       b=int(input("enter no. of elements you wanna insert in stack: "))
       for i in range(b):
             c=int(input("enter element " + str(i+1) + ": "))
             list.append(c)
       print(list)
       print()
elif a==2:
Because the print lines are part of the conditional code block.
 
Share this answer
 
Comments
akshaychauhan0101 27-Jul-22 5:33am    
Thank you, OriginalGriff. Much appreciated!
OriginalGriff 27-Jul-22 6:02am    
You're welcome!
CPallini 27-Jul-22 5:37am    
5.
Quote:
there's an error I'm getting which I have posted below the code.

In Python, indentation is the programm structure.
Python
if a==1: # this if
       b=int(input("enter no. of elements you wanna insert in stack: "))
       for i in range(b):
             c=int(input("enter element " + str(i+1) + ": "))
             list.append(c)
print(list) # ends here
print()
elif a==2: # thus, this is not linked to previous if

But this is ok
Python
if a==1:
       b=int(input("enter no. of elements you wanna insert in stack: "))
       for i in range(b):
             c=int(input("enter element " + str(i+1) + ": "))
             list.append(c)
       print(list)
       print()
elif a==2:

if and elif are at same level and there is nothing else at same level in between.
 
Share this answer
 
Comments
akshaychauhan0101 27-Jul-22 5:34am    
Thank you, Patrice T. Much appreciated!
CPallini 27-Jul-22 5:37am    
5.
Patrice T 27-Jul-22 6:16am    
Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900