我正在为经验公式编写语法控制器。它似乎工作得相当好,但有一个我似乎无法解决的错误。
以下是代码:
import string
from linkedQFile import LinkedQ
ATOMER = ["H","He","Li","Be","B","C","N","O","F","Ne","Na","Mg","Al","Si","P","S","Cl","Ar"]
class Formelfel(Exception):
pass
def readFormel():
if q.peek() == None:
print("Formel saknas. ")
readMol()
print("Formeln är syntaktiskt korrekt")
#newline check?
def readMol():
if q.peek() == None:
return
readGroup()
readMol()
return
def readGroup():
if q.peek() == "(":
print(q.get(), end="")
if q.peek() in string.ascii_lowercase or q.peek() in string.ascii_uppercase:
readMol()
else:
raise Formelfel
if q.peek() == ")":
print(q.get(), end="")
readNum()
return
readAtom()
try:
readNum()
except Formelfel:
pass
return
def readAtom():
X = readLetter()
try:
x = readletter()
except Formelfel:
x = ""
atom = X+x
if atom in ATOMER:
return
rest=""
while not q.isEmpty():
rest += print(q.get(), end="")
raise Formelfel("Okänd atom vid radslutet "+rest)
def readNum():
try:
if q.peek() != None:
print(q.peek())
while not q.peek() in string.ascii_uppercase and not q.peek() in string.ascii_lowercase and not q.peek() == "(" and not q.peek()==")": # If q.peek() is a number
if q.peek() == None:
return
int(q.get())
return
except:
raise Formelfel
def readLetter():
if q.peek() in string.ascii_uppercase:
x =q.get()
print(x,end="")
return x
raise Formelfel
def readletter():
if q.peek() == None:
raise Formelfel
if q.peek() in string.ascii_lowercase:
x =q.get()
print(x,end= "")
return x
raise Formelfel
formel= "Si(C3(COOH)2)4(H2O)7"
q = LinkedQ()
for symbol in formel:
q.put(symbol)
readFormel()
我收到以下输出和错误:
Si(
(C3
(CO
OO
OH
H)
)2
)4
(H2
O)
)7
Traceback (most recent call last):
File "formelkoll.py", line 59, in readNum
while not q.peek() in string.ascii_uppercase and not q.peek() in string.ascii_lowercase and not q.peek() == "(" and not q.peek()==")": # If q.peek() is a number
TypeError: 'in <string>' requires string as left operand, not NoneType
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "formelkoll.py", line 88, in <module>
readFormel()
File "formelkoll.py", line 12, in readFormel
readMol()
File "formelkoll.py", line 20, in readMol
readMol()
File "formelkoll.py", line 19, in readMol
readGroup()
File "formelkoll.py", line 27, in readGroup
readMol()
File "formelkoll.py", line 20, in readMol
readMol()
File "formelkoll.py", line 19, in readMol
readGroup()
File "formelkoll.py", line 27, in readGroup
readMol()
File "formelkoll.py", line 20, in readMol
readMol()
File "formelkoll.py", line 20, in readMol
readMol()
File "formelkoll.py", line 20, in readMol
readMol()
File "formelkoll.py", line 20, in readMol
readMol()
File "formelkoll.py", line 20, in readMol
readMol()
File "formelkoll.py", line 20, in readMol
readMol()
File "formelkoll.py", line 19, in readMol
readGroup()
File "formelkoll.py", line 27, in readGroup
readMol()
File "formelkoll.py", line 20, in readMol
readMol()
File "formelkoll.py", line 20, in readMol
readMol()
File "formelkoll.py", line 19, in readMol
readGroup()
File "formelkoll.py", line 32, in readGroup
readNum()
File "formelkoll.py", line 65, in readNum
raise Formelfel
__main__.Formelfel
我不明白为什么会出现这种错误,因为我有一个if语句:
if q.peek() != None:
发生错误的行之前。换句话说,为什么 None 允许通过这个if语句?
答案 0 :(得分:0)
我明白了。正如评论者所说,调用q.get(),它会改变q.peek()的值。两行的切换解决了这个问题。
def readNum():
try:
if q.peek() != None: #Vrf får vi error NoneType trots detta??
print(q.peek())
while not q.peek() in string.ascii_uppercase and not q.peek() in string.ascii_lowercase and not q.peek() == "(" and not q.peek()==")":
int(q.get())
if q.peek() == None:
return
return
except:
raise Formelfel