我試圖用交互輸入來創建一個函數來告訴你什麼'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)
<ipython-input-2-20b88ae22368> 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:本地變量賦值
如果未執行'if'塊,則'a'將會被定義爲 –
@MosesKoledoye在這種情況下,爲什麼'if/elif'不被執行,儘管它在我的函數下? – vluos
大概價值既不是Z也不是E. –