![](/img/trans.png)
[英]Basic Python Issue (UnboundLocalError: local variable 'normal' referenced before assignment)
[英]Python “local variable 'pc' referenced before assignment” issue in basic decomplier
一個朋友和我本人正在努力創建一個基本的概念驗證反編譯器,該反編譯器將接受一串十六進制值並返回更具可讀性的版本。 我們的代碼在下面列出
testProgram = "00 00 FF 55 47 00"
# should look like this
# NOP
# NOP
# MOV 55 47
# NOP
pc = 0
output = ""
def byte(int):
return testProgram[3 * int:3 * int + 2]
def interpret():
currentByte = byte(pc)
if currentByte == "00":
pc += 1
return "NOP"
if currentByte == "FF":
returner = "MOV " + byte(pc + 1) + " " + byte(pc + 2)
pc += 3
return returner
while(byte(pc) != ""):
output += interpret() + "\n"
print(output)
但是,運行代碼可以告訴我們
Traceback (most recent call last):
File "BasicTest.py", line 62, in <module>
output += interpret() + "\n"
File "BasicTest.py", line 50, in interpret
currentByte = byte(pc)
UnboundLocalError: local variable 'pc' referenced before assignment
因為pc是全局變量,所以它不應該在任何地方都可以使用嗎? 感謝您提供所有幫助-如果發現其他錯誤,請隨時發表評論指出錯誤!
最近經常看到這種情況。 當你做
if currentByte == "00":
pc += 1 # <----------
return "NOP"
您正在分配給局部變量pc
,但是在局部作用域中尚未聲明pc
。 如果要修改全局pc
,則需要在函數頂部顯式聲明
global pc
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.