我试图用交互输入来创建一个函数来告诉你什么'SMILES'公式是脂肪酸(化合物),但我一直得到这个错误:python错误:在赋值之前引用的局部变量'a'
def fatty_gen(chain_length, db_position, db_orientation):
"Returns the SMILES code of the fatty acid, given its chain length, db position, db orientation"
chain_length=input("What is the chain length/number of C?")
chain_length2=int(chain_length)
db_position = input("On which carbon does the double bond first appear")
db_position2=int(db_position)
db_orientation= input("What is the orientation of the double bond")
db_orientation2=str(db_orientation)
if db_orientation2 =="Z":
a="/C=C\\"
elif db_orientation2=="E":
a="\C=C\\"
else: a =""
return "C"*((db_position2)-1) + a + "C"*(chain_length2-db_position2-1)
in fatty_gen(chain_length, db_position, db_orientation)
13 a="\C=C\\"
14
---> 15 return "C"*((db_position2)-1) + a + "C"*(chain_length2-db_position2-1)
16 fatty_gen(1,1,1)
UnboundLocalError: local variable 'a' referenced before assignment
UnboundLocalError:本地变量赋值
2016-10-15
vluos
+1
如果未执行'if'块,则'a'将会被定义为 –
+0
@MosesKoledoye在这种情况下,为什么'if/elif'不被执行,尽管它在我的函数下? –
+1
大概价值既不是Z也不是E. –