尝试停止python中Stacks的错误 [英] Trying to stop an error with Stacks in python
问题描述
我正在尝试编写一个程序来检查以确保文本在方括号(so(),[],{}而不是(),[,{})方面是平衡的。当它处于平衡状态时,或者缺少一个右括号时,如果它不平衡,我就可以使其工作(如前面的示例)。如果我错过了左侧的括号((),],{}),我将无能为力。我知道它正在尝试从空的堆栈中弹出,但无法弄清楚如何应对。我的老师在她的Stack班级中有一个提示,如果它试图弹出一个空堆栈,则会自动引发一个异常,而我不能更改她的班级,这就是问题所在,否则无论如何我都会将其设置为false而不是陷入混乱。那么,有人会在出现该错误之前对如何执行操作有任何想法吗?
这是代码:
I'm trying to make a program that checks to make sure that the text is balanced in terms of brackets (so (),[],{} and not (),[,{}). I can get it to work when it is balanced, and when it is not balanced when it is missing a closing bracket (like the previous example). What I can't get it to do is come back as unbalanced if I'm missing a bracket on the left ((),],{}). I know it's trying to pop from an empty stack but can't figure out how to counter act that. My teacher has it in her Stack class that if it's trying to pop to an empty stack, then an exception is raised automatically, and I can't change her class, which is the problem, otherwise I just would have made that as false anyways and not be in this mess. So does anyone have any ideas of how to do it before that error is raised? Here's the code:
from ListNode import *
from Stack import Stack
ch = 0
s = 0
check = True
def parbalance():
stack = Stack()
user = input("Enter a file name: ")
file = open(user)
lines = file.readlines()
for char in lines:
for ch in char:
#print(ch)
if ch in "([{":
stack.push(ch)
if ch in ")]}":
popStack = stack.pop()
if ch == "(" and popStack != ")":
check = False
elif ch == "[" and popStack != "]":
check = False
elif ch == "{" and popStack != "}":
check = False
if stack.is_empty():
check = True
print("true")
else:
check = False
print("false")
parbalance()
在如果有帮助,这里是她的Stack类:
In case it helps, here's her Stack class:
from ListNode import *
class Stack:
def __init__(self):
self.top = None
def push(self, item):
temp = ListNode(item)
temp.set_link(self.top)
self.top = temp
#self.top = ListNode(item, self.top)
def pop(self):
if self.top == None:
raise Exception("Trying to pop from an empty stack")
temp = self.top
self.top = temp.get_link()
return temp.get_item()
def destroy(self):
self.top = None
def is_full(self):
return False
def is_empty(self):
return self.top == None
推荐答案
使用尝试
捕获错误:
try:
popStack = stack.pop()
except:
# Stack is empty, set failure and bail from the function.
check = False
return
此外,请注意,您的测试是倒退的:
Also, note that your tests are backwards:
if ch == "(" and popStack != ")":
ch
是右括号, popStack
是开括号,因此应为:
ch
is the closing bracket and popStack
is the opening bracket, so this should be:
if ch == ")" and popStack != "(":
如果不进行此更改,您的代码将识别字符串(}
平衡。
Without this change, your code will recognize the string "(}"
as balanced.
作为旁注,请考虑返回 True
或 False
而不是设置全局变量。使用全局变量从函数返回值不是一个好主意。
As a side note, consider returning True
or False
from the function instead of setting a global variable. Using global variables to return values from functions is not a good idea.
这篇关于尝试停止python中Stacks的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!