亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

python 中的變量賦值錯誤

python 中的變量賦值錯誤

慕哥9229398 2022-08-16 10:33:26
我得到了一個變量賦值錯誤,顯然沒有理解一些東西,但從我的代碼中,我不明白為什么會發生這種情況。代碼:   def something(filename):    with open("tmp/"+filename.stem+".txt", "r") as infile: # opening the tmp file for scraping the data        readfile = infile.readlines() #reads the infile lne by line and returns a list containing the lines        for i, line in enumerate(readfile[1:], 1): # looping over all the lines in the file from position 1 (so skipping 0) to avoid circular feedback            if 'Overview' in line:                start = i                continue                for i, line in enumerate(readfile[1:], 1):                    if 'Identified secondary metabolite regions using strictness' in line:                        end = i                        marker = list(map(lambda s: s.strip('\n'), readfile[start + 1:end])) # stripping the '\n' off every element in the list. map executes a function for each element in a sequence        for i, line in enumerate(readfile[1:], 1): # looping over all the lines in the file from position 1 (so skipping 0) to avoid circular feedback            for location in marker:錯誤:UnboundLocalError: local variable 'marker' referenced before assignment由于第一個 forloop 在第二個 forloop 之前執行導致被設置,因此不應始終在函數范圍內進行賦值?markermarker其次,使用兩個for循環似乎很混亂,我如何在一個for循環中執行此操作?
查看完整描述

3 回答

?
Smart貓小萌

TA貢獻1911條經驗 獲得超7個贊

關鍵是,它是在“如果”條件下分配的。如果它是假的呢?marker



查看完整回答
反對 回復 2022-08-16
?
縹緲止盈

TA貢獻2041條經驗 獲得超4個贊

嘗試在函數外部定義標記變量。然后在功能中使用它。

refer-> 不明白為什么 UnboundLocalError 會發生


查看完整回答
反對 回復 2022-08-16
?
忽然笑

TA貢獻1806條經驗 獲得超5個贊

marker在不滿足條件時未設置。設置在開頭。if 'Identified secondary metabolite regions using strictness' in line:marker = []



查看完整回答
反對 回復 2022-08-16
  • 3 回答
  • 0 關注
  • 228 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號