+ 3
Why the following code gives error?[solved]
4 Réponses
+ 9
There are several indentation errors in while loop, also a closing bracket in line 20 is missing. Anshika , did you try to fix this issues by yourself? This is one of the key abilities that coders should cover. Try to read and understand the error messages, and olso try to fix issue by issue.
+ 4
Jayakrishna🇮🇳
Lothar
Thank you very much for telling me the problems in the code😊 .Now ,I had made some changes, please check it out. And as in this app we cannot give the input multiple times in python, that's why I will suggest you to give similar inputs like I have given in below examples-
(Firstly, you have to enter the length of your stack. Then, you have to choose the operation you want to perform on that stack. There are three operations
1.push
2.pop
3.stop
And then the code will perform that operation.)
Examples--(first)
4
push
1
push
2
push
3
pop
stop
Example--(second)
2
push
3
pop
Example--(third)
3
push
2
push
4
stop
+ 1
Anshika
Your not declared space but using it..
I don't know why and you using eval in item =eval(input(" enter") but you missing to closing it.. Add.. ).
You are opting to enter" pop" but comparing opt=="pull" ,.
And add while loop exit condition, otherwise it is Infinite loop....
Make these changes and try...
Read error carefully to get idea, what going wrong... Hope it helps....
+ 1
Anshika yes. Working now correctly...
Some points I want to add few points..
Instead of breaking loop when stack is full, allow pop operation..
Add an another else to say invalid input.. and break or continue by other chance.. If user enter invalid input....
And you are adding end='\n' in print, which is no need. The default value of ' end' in print is \n. So you don't need to add. But if you want to remove \n or add anything, then just override its value by end= "" or end ="|"..
So print(operation) is same as print(operation, end="\n")
Working for all your inputs. Your 2nd example has no stop condition..
Reply, if any doubt in my post..
Happy coding...